Add error message
Add error message when trying to relate primitives to the boxed/apparent backing types.
This commit is contained in:
parent
d8ab098195
commit
806e142cbf
15 changed files with 95 additions and 1 deletions
|
@ -6258,13 +6258,20 @@ namespace ts {
|
|||
targetType = typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
|
||||
}
|
||||
|
||||
// check if trying to relate primitives to the boxed/apparent backing types.
|
||||
if ((sourceType === "Number" && targetType === "number") ||
|
||||
(sourceType === "String" && targetType === "string") ||
|
||||
(sourceType === "Boolean" && targetType === "boolean")) {
|
||||
reportError(Diagnostics._0_is_a_primitive_type_while_1_is_a_boxed_object_Prefer_using_0_when_possible, targetType, sourceType);
|
||||
}
|
||||
|
||||
if (!message) {
|
||||
message = relation === comparableRelation ?
|
||||
Diagnostics.Type_0_is_not_comparable_to_type_1 :
|
||||
Diagnostics.Type_0_is_not_assignable_to_type_1;
|
||||
}
|
||||
|
||||
reportError(message, sourceType, targetType);
|
||||
reportError(message, sourceType, targetType);
|
||||
}
|
||||
|
||||
// Compare two types and return
|
||||
|
|
|
@ -1955,6 +1955,10 @@
|
|||
"category": "Error",
|
||||
"code": 2691
|
||||
},
|
||||
"'{0}' is a primitive type while '{1}' is a boxed object. Prefer using '{0}' when possible.": {
|
||||
"category": "Error",
|
||||
"code": 2692
|
||||
},
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
"code": 4000
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts(9,7): error TS2415: Class 'Derived<U>' incorrectly extends base class 'Base<string>'.
|
||||
Types of property 'x' are incompatible.
|
||||
Type 'String' is not assignable to type 'string'.
|
||||
'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts (1 errors) ====
|
||||
|
@ -17,6 +18,7 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtypi
|
|||
!!! error TS2415: Class 'Derived<U>' incorrectly extends base class 'Base<string>'.
|
||||
!!! error TS2415: Types of property 'x' are incompatible.
|
||||
!!! error TS2415: Type 'String' is not assignable to type 'string'.
|
||||
!!! error TS2415: 'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
x: String;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSuperty
|
|||
Types of property 'x' are incompatible.
|
||||
Type 'U' is not assignable to type 'string'.
|
||||
Type 'String' is not assignable to type 'string'.
|
||||
'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSupertype.ts (1 errors) ====
|
||||
|
@ -19,5 +20,6 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSuperty
|
|||
!!! error TS2415: Types of property 'x' are incompatible.
|
||||
!!! error TS2415: Type 'U' is not assignable to type 'string'.
|
||||
!!! error TS2415: Type 'String' is not assignable to type 'string'.
|
||||
!!! error TS2415: 'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
x: U;
|
||||
}
|
|
@ -18,6 +18,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
|
|||
Types of parameters 'items' and 'items' are incompatible.
|
||||
Type 'Number' is not assignable to type 'string | number'.
|
||||
Type 'Number' is not assignable to type 'number'.
|
||||
'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
|
||||
|
||||
==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (6 errors) ====
|
||||
|
@ -81,4 +82,5 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
|
|||
!!! error TS2322: Types of parameters 'items' and 'items' are incompatible.
|
||||
!!! error TS2322: Type 'Number' is not assignable to type 'string | number'.
|
||||
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
!!! error TS2322: 'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts(3,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
|
||||
'boolean' is a primitive type while 'Boolean' is a boxed object. Prefer using 'boolean' when possible.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts (1 errors) ====
|
||||
|
@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts(3
|
|||
x = a;
|
||||
~
|
||||
!!! error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
|
||||
!!! error TS2322: 'boolean' is a primitive type while 'Boolean' is a boxed object. Prefer using 'boolean' when possible.
|
||||
a = x;
|
|
@ -3,6 +3,7 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(
|
|||
Type '() => Object' is not assignable to type '() => boolean'.
|
||||
Type 'Object' is not assignable to type 'boolean'.
|
||||
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(19,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
|
||||
'boolean' is a primitive type while 'Boolean' is a boxed object. Prefer using 'boolean' when possible.
|
||||
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(20,1): error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'.
|
||||
|
||||
|
||||
|
@ -33,6 +34,7 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface2.ts(
|
|||
x = a; // expected error
|
||||
~
|
||||
!!! error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
|
||||
!!! error TS2322: 'boolean' is a primitive type while 'Boolean' is a boxed object. Prefer using 'boolean' when possible.
|
||||
x = b; // expected error
|
||||
~
|
||||
!!! error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts(3,1): error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts (1 errors) ====
|
||||
|
@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts(3,1
|
|||
x = a;
|
||||
~
|
||||
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
!!! error TS2322: 'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
a = x;
|
|
@ -1,4 +1,5 @@
|
|||
tests/cases/conformance/types/primitives/number/assignFromNumberInterface2.ts(24,1): error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
tests/cases/conformance/types/primitives/number/assignFromNumberInterface2.ts(25,1): error TS2322: Type 'NotNumber' is not assignable to type 'number'.
|
||||
|
||||
|
||||
|
@ -29,6 +30,7 @@ tests/cases/conformance/types/primitives/number/assignFromNumberInterface2.ts(25
|
|||
x = a; // expected error
|
||||
~
|
||||
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
!!! error TS2322: 'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
x = b; // expected error
|
||||
~
|
||||
!!! error TS2322: Type 'NotNumber' is not assignable to type 'number'.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts(3,1): error TS2322: Type 'String' is not assignable to type 'string'.
|
||||
'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts (1 errors) ====
|
||||
|
@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts(3,1
|
|||
x = a;
|
||||
~
|
||||
!!! error TS2322: Type 'String' is not assignable to type 'string'.
|
||||
!!! error TS2322: 'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
a = x;
|
|
@ -1,4 +1,5 @@
|
|||
tests/cases/conformance/types/primitives/string/assignFromStringInterface2.ts(47,1): error TS2322: Type 'String' is not assignable to type 'string'.
|
||||
'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
tests/cases/conformance/types/primitives/string/assignFromStringInterface2.ts(48,1): error TS2322: Type 'NotString' is not assignable to type 'string'.
|
||||
|
||||
|
||||
|
@ -52,6 +53,7 @@ tests/cases/conformance/types/primitives/string/assignFromStringInterface2.ts(48
|
|||
x = a; // expected error
|
||||
~
|
||||
!!! error TS2322: Type 'String' is not assignable to type 'string'.
|
||||
!!! error TS2322: 'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
x = b; // expected error
|
||||
~
|
||||
!!! error TS2322: Type 'NotString' is not assignable to type 'string'.
|
||||
|
|
29
tests/baselines/reference/nativeToBoxedTypes.errors.txt
Normal file
29
tests/baselines/reference/nativeToBoxedTypes.errors.txt
Normal file
|
@ -0,0 +1,29 @@
|
|||
tests/cases/compiler/nativeToBoxedTypes.ts(3,1): error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
tests/cases/compiler/nativeToBoxedTypes.ts(7,1): error TS2322: Type 'String' is not assignable to type 'string'.
|
||||
'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
tests/cases/compiler/nativeToBoxedTypes.ts(11,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
|
||||
'boolean' is a primitive type while 'Boolean' is a boxed object. Prefer using 'boolean' when possible.
|
||||
|
||||
|
||||
==== tests/cases/compiler/nativeToBoxedTypes.ts (3 errors) ====
|
||||
var N = new Number();
|
||||
var n = 100;
|
||||
n = N;
|
||||
~
|
||||
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
!!! error TS2322: 'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
|
||||
var S = new String();
|
||||
var s = "foge";
|
||||
s = S;
|
||||
~
|
||||
!!! error TS2322: Type 'String' is not assignable to type 'string'.
|
||||
!!! error TS2322: 'string' is a primitive type while 'String' is a boxed object. Prefer using 'string' when possible.
|
||||
|
||||
var B = new Boolean();
|
||||
var b = true;
|
||||
b = B;
|
||||
~
|
||||
!!! error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
|
||||
!!! error TS2322: 'boolean' is a primitive type while 'Boolean' is a boxed object. Prefer using 'boolean' when possible.
|
23
tests/baselines/reference/nativeToBoxedTypes.js
Normal file
23
tests/baselines/reference/nativeToBoxedTypes.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
//// [nativeToBoxedTypes.ts]
|
||||
var N = new Number();
|
||||
var n = 100;
|
||||
n = N;
|
||||
|
||||
var S = new String();
|
||||
var s = "foge";
|
||||
s = S;
|
||||
|
||||
var B = new Boolean();
|
||||
var b = true;
|
||||
b = B;
|
||||
|
||||
//// [nativeToBoxedTypes.js]
|
||||
var N = new Number();
|
||||
var n = 100;
|
||||
n = N;
|
||||
var S = new String();
|
||||
var s = "foge";
|
||||
s = S;
|
||||
var B = new Boolean();
|
||||
var b = true;
|
||||
b = B;
|
|
@ -1,5 +1,6 @@
|
|||
tests/cases/compiler/primitiveMembers.ts(5,3): error TS2339: Property 'toBAZ' does not exist on type 'number'.
|
||||
tests/cases/compiler/primitiveMembers.ts(11,1): error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
|
||||
|
||||
==== tests/cases/compiler/primitiveMembers.ts (2 errors) ====
|
||||
|
@ -18,6 +19,7 @@ tests/cases/compiler/primitiveMembers.ts(11,1): error TS2322: Type 'Number' is n
|
|||
n = N; // should not work, as 'number' has a different brand
|
||||
~
|
||||
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
|
||||
!!! error TS2322: 'number' is a primitive type while 'Number' is a boxed object. Prefer using 'number' when possible.
|
||||
N = n; // should work
|
||||
|
||||
var o: Object = {}
|
||||
|
|
11
tests/cases/compiler/nativeToBoxedTypes.ts
Normal file
11
tests/cases/compiler/nativeToBoxedTypes.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
var N = new Number();
|
||||
var n = 100;
|
||||
n = N;
|
||||
|
||||
var S = new String();
|
||||
var s = "foge";
|
||||
s = S;
|
||||
|
||||
var B = new Boolean();
|
||||
var b = true;
|
||||
b = B;
|
Loading…
Reference in a new issue