Skip to content

Commit ddb5a00

Browse files
authored
Merge pull request #10446 from YuichiNukiyama/fix10351
Add error message
2 parents 93de502 + 590755b commit ddb5a00

16 files changed

+123
-1
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6300,6 +6300,18 @@ namespace ts {
63006300
reportError(message, sourceType, targetType);
63016301
}
63026302

6303+
function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
6304+
const sourceType = typeToString(source);
6305+
const targetType = typeToString(target);
6306+
6307+
if ((globalStringType === source && stringType === target) ||
6308+
(globalNumberType === source && numberType === target) ||
6309+
(globalBooleanType === source && booleanType === target) ||
6310+
(getGlobalESSymbolType() === source && esSymbolType === target)) {
6311+
reportError(Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType);
6312+
}
6313+
}
6314+
63036315
// Compare two types and return
63046316
// Ternary.True if they are related with no assumptions,
63056317
// Ternary.Maybe if they are related with assumptions of other relationships, or
@@ -6423,6 +6435,9 @@ namespace ts {
64236435
}
64246436

64256437
if (reportErrors) {
6438+
if (source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.Primitive) {
6439+
tryElaborateErrorsForPrimitivesAndObjects(source, target);
6440+
}
64266441
reportRelationError(headMessage, source, target);
64276442
}
64286443
return Ternary.False;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,10 @@
19551955
"category": "Error",
19561956
"code": 2691
19571957
},
1958+
"'{0}' is a primitive, but '{1}' is a wrapper object. Prefer using '{0}' when possible.": {
1959+
"category": "Error",
1960+
"code": 2692
1961+
},
19581962
"Import declaration '{0}' is using private name '{1}'.": {
19591963
"category": "Error",
19601964
"code": 4000

tests/baselines/reference/apparentTypeSubtyping.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts(9,7): error TS2415: Class 'Derived<U>' incorrectly extends base class 'Base<string>'.
22
Types of property 'x' are incompatible.
33
Type 'String' is not assignable to type 'string'.
4+
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
45

56

67
==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts (1 errors) ====
@@ -17,6 +18,7 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtypi
1718
!!! error TS2415: Class 'Derived<U>' incorrectly extends base class 'Base<string>'.
1819
!!! error TS2415: Types of property 'x' are incompatible.
1920
!!! error TS2415: Type 'String' is not assignable to type 'string'.
21+
!!! error TS2415: 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
2022
x: String;
2123
}
2224

tests/baselines/reference/apparentTypeSupertype.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSuperty
22
Types of property 'x' are incompatible.
33
Type 'U' is not assignable to type 'string'.
44
Type 'String' is not assignable to type 'string'.
5+
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
56

67

78
==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSupertype.ts (1 errors) ====
@@ -19,5 +20,6 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSuperty
1920
!!! error TS2415: Types of property 'x' are incompatible.
2021
!!! error TS2415: Type 'U' is not assignable to type 'string'.
2122
!!! error TS2415: Type 'String' is not assignable to type 'string'.
23+
!!! error TS2415: 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
2224
x: U;
2325
}

tests/baselines/reference/arrayLiterals3.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
1717
Types of parameters 'items' and 'items' are incompatible.
1818
Type 'Number' is not assignable to type 'string | number'.
1919
Type 'Number' is not assignable to type 'number'.
20+
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
2021

2122

2223
==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (6 errors) ====
@@ -79,4 +80,5 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
7980
!!! error TS2322: Types of parameters 'items' and 'items' are incompatible.
8081
!!! error TS2322: Type 'Number' is not assignable to type 'string | number'.
8182
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
83+
!!! error TS2322: 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
8284

tests/baselines/reference/assignFromBooleanInterface.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts(3,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
2+
'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
23

34

45
==== tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts (1 errors) ====
@@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts(3
78
x = a;
89
~
910
!!! error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
11+
!!! error TS2322: 'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
1012
a = x;

tests/baselines/reference/assignFromBooleanInterface2.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(
33
Type '() => Object' is not assignable to type '() => boolean'.
44
Type 'Object' is not assignable to type 'boolean'.
55
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(19,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
6+
'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
67
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(20,1): error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'.
78

89

@@ -33,6 +34,7 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(
3334
x = a; // expected error
3435
~
3536
!!! error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
37+
!!! error TS2322: 'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
3638
x = b; // expected error
3739
~
3840
!!! error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'.

tests/baselines/reference/assignFromNumberInterface.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts(3,1): error TS2322: Type 'Number' is not assignable to type 'number'.
2+
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
23

34

45
==== tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts (1 errors) ====
@@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts(3,1
78
x = a;
89
~
910
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
11+
!!! error TS2322: 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
1012
a = x;

tests/baselines/reference/assignFromNumberInterface2.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests/cases/conformance/types/primitives/number/assignFromNumberInterface2.ts(24,1): error TS2322: Type 'Number' is not assignable to type 'number'.
2+
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
23
tests/cases/conformance/types/primitives/number/assignFromNumberInterface2.ts(25,1): error TS2322: Type 'NotNumber' is not assignable to type 'number'.
34

45

@@ -29,6 +30,7 @@ tests/cases/conformance/types/primitives/number/assignFromNumberInterface2.ts(25
2930
x = a; // expected error
3031
~
3132
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
33+
!!! error TS2322: 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
3234
x = b; // expected error
3335
~
3436
!!! error TS2322: Type 'NotNumber' is not assignable to type 'number'.

tests/baselines/reference/assignFromStringInterface.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts(3,1): error TS2322: Type 'String' is not assignable to type 'string'.
2+
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
23

34

45
==== tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts (1 errors) ====
@@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts(3,1
78
x = a;
89
~
910
!!! error TS2322: Type 'String' is not assignable to type 'string'.
11+
!!! error TS2322: 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
1012
a = x;

0 commit comments

Comments
 (0)