Skip to content

Add error message #10446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6267,6 +6267,18 @@ namespace ts {
reportError(message, sourceType, targetType);
}

function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
const sourceType = typeToString(source);
const targetType = typeToString(target);

if ((globalStringType === source && stringType === target) ||
(globalNumberType === source && numberType === target) ||
(globalBooleanType === source && booleanType === target) ||
(getGlobalESSymbolType() === source && esSymbolType === target)) {
reportError(Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType);
}
}

// Compare two types and return
// Ternary.True if they are related with no assumptions,
// Ternary.Maybe if they are related with assumptions of other relationships, or
Expand Down Expand Up @@ -6390,6 +6402,9 @@ namespace ts {
}

if (reportErrors) {
if (source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.Primitive) {
tryElaborateErrorsForPrimitivesAndObjects(source, target);
}
reportRelationError(headMessage, source, target);
}
return Ternary.False;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,10 @@
"category": "Error",
"code": 2691
},
"'{0}' is a primitive, but '{1}' is a wrapper object. Prefer using '{0}' when possible.": {
"category": "Error",
"code": 2692
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/apparentTypeSubtyping.errors.txt
Original file line number Diff line number Diff line change
@@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.


==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts (1 errors) ====
Expand All @@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.
x: String;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/apparentTypeSupertype.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.


==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSupertype.ts (1 errors) ====
Expand All @@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.
x: U;
}
2 changes: 2 additions & 0 deletions tests/baselines/reference/arrayLiterals3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.


==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (6 errors) ====
Expand Down Expand Up @@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.

Original file line number Diff line number Diff line change
@@ -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, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.


==== tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts (1 errors) ====
Expand All @@ -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, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
a = x;
Original file line number Diff line number Diff line change
Expand Up @@ -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, but 'Boolean' is a wrapper 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'.


Expand Down Expand Up @@ -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, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
x = b; // expected error
~
!!! error TS2322: Type 'NotBoolean' is not assignable to type 'boolean'.
Expand Down
Original file line number Diff line number Diff line change
@@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.


==== tests/cases/conformance/types/primitives/number/assignFromNumberInterface.ts (1 errors) ====
Expand All @@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.
a = x;
Original file line number Diff line number Diff line change
@@ -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, but 'Number' is a wrapper 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'.


Expand Down Expand Up @@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.
x = b; // expected error
~
!!! error TS2322: Type 'NotNumber' is not assignable to type 'number'.
Expand Down
Original file line number Diff line number Diff line change
@@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.


==== tests/cases/conformance/types/primitives/string/assignFromStringInterface.ts (1 errors) ====
Expand All @@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.
a = x;
Original file line number Diff line number Diff line change
@@ -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, but 'String' is a wrapper 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'.


Expand Down Expand Up @@ -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, but 'String' is a wrapper object. Prefer using 'string' when possible.
x = b; // expected error
~
!!! error TS2322: Type 'NotString' is not assignable to type 'string'.
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/nativeToBoxedTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tests/cases/compiler/nativeToBoxedTypes.ts(3,1): error TS2322: Type 'Number' is not assignable to type 'number'.
'number' is a primitive, but 'Number' is a wrapper 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, but 'String' is a wrapper 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, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
tests/cases/compiler/nativeToBoxedTypes.ts(14,10): error TS2304: Cannot find name 'Symbol'.


==== tests/cases/compiler/nativeToBoxedTypes.ts (4 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, but 'Number' is a wrapper 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, but 'String' is a wrapper 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, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.

var sym: symbol;
var Sym: Symbol;
~~~~~~
!!! error TS2304: Cannot find name 'Symbol'.
sym = Sym;
30 changes: 30 additions & 0 deletions tests/baselines/reference/nativeToBoxedTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [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;

var sym: symbol;
var Sym: Symbol;
sym = Sym;

//// [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;
var sym;
var Sym;
sym = Sym;
2 changes: 2 additions & 0 deletions tests/baselines/reference/primitiveMembers.errors.txt
Original file line number Diff line number Diff line change
@@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.


==== tests/cases/compiler/primitiveMembers.ts (2 errors) ====
Expand All @@ -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, but 'Number' is a wrapper object. Prefer using 'number' when possible.
N = n; // should work

var o: Object = {}
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/symbolType15.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tests/cases/conformance/es6/Symbols/symbolType15.ts(5,1): error TS2322: Type 'Symbol' is not assignable to type 'symbol'.
'symbol' is a primitive, but 'Symbol' is a wrapper object. Prefer using 'symbol' when possible.


==== tests/cases/conformance/es6/Symbols/symbolType15.ts (1 errors) ====
Expand All @@ -8,4 +9,5 @@ tests/cases/conformance/es6/Symbols/symbolType15.ts(5,1): error TS2322: Type 'Sy
symObj = sym;
sym = symObj;
~~~
!!! error TS2322: Type 'Symbol' is not assignable to type 'symbol'.
!!! error TS2322: Type 'Symbol' is not assignable to type 'symbol'.
!!! error TS2322: 'symbol' is a primitive, but 'Symbol' is a wrapper object. Prefer using 'symbol' when possible.
15 changes: 15 additions & 0 deletions tests/cases/compiler/nativeToBoxedTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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;

var sym: symbol;
var Sym: Symbol;
sym = Sym;