Skip to content

Commit 0c01874

Browse files
follow advise
1 parent 37a9e6a commit 0c01874

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6258,13 +6258,6 @@ namespace ts {
62586258
targetType = typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
62596259
}
62606260

6261-
// check if trying to relate primitives to the boxed/apparent backing types.
6262-
if ((sourceType === "Number" && targetType === "number") ||
6263-
(sourceType === "String" && targetType === "string") ||
6264-
(sourceType === "Boolean" && targetType === "boolean")) {
6265-
reportError(Diagnostics._0_is_a_primitive_type_while_1_is_a_boxed_object_Prefer_using_0_when_possible, targetType, sourceType);
6266-
}
6267-
62686261
if (!message) {
62696262
message = relation === comparableRelation ?
62706263
Diagnostics.Type_0_is_not_comparable_to_type_1 :
@@ -6274,6 +6267,19 @@ namespace ts {
62746267
reportError(message, sourceType, targetType);
62756268
}
62766269

6270+
function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
6271+
const sourceType = typeToString(source);
6272+
const targetType = typeToString(target);
6273+
6274+
if ((globalStringType === source && stringType === target) ||
6275+
(globalNumberType === source && numberType === target) ||
6276+
(globalBooleanType === source && booleanType === target) ||
6277+
(getGlobalESSymbolType() === source && esSymbolType === target)) {
6278+
console.log(source);console.log(target);
6279+
reportError(Diagnostics._0_is_a_primitive_type_while_1_is_a_boxed_object_Prefer_using_0_when_possible, targetType, sourceType);
6280+
}
6281+
}
6282+
62776283
// Compare two types and return
62786284
// Ternary.True if they are related with no assumptions,
62796285
// Ternary.Maybe if they are related with assumptions of other relationships, or
@@ -6396,6 +6402,10 @@ namespace ts {
63966402
}
63976403
}
63986404

6405+
if (source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.Primitive) {
6406+
tryElaborateErrorsForPrimitivesAndObjects(source, target);
6407+
}
6408+
63996409
if (reportErrors) {
64006410
reportRelationError(headMessage, source, target);
64016411
}

tests/baselines/reference/symbolType15.errors.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests/cases/conformance/es6/Symbols/symbolType15.ts(5,1): error TS2322: Type 'Symbol' is not assignable to type 'symbol'.
2+
'symbol' is a primitive type while 'Symbol' is a boxed object. Prefer using 'symbol' when possible.
23

34

45
==== tests/cases/conformance/es6/Symbols/symbolType15.ts (1 errors) ====
@@ -8,4 +9,5 @@ tests/cases/conformance/es6/Symbols/symbolType15.ts(5,1): error TS2322: Type 'Sy
89
symObj = sym;
910
sym = symObj;
1011
~~~
11-
!!! error TS2322: Type 'Symbol' is not assignable to type 'symbol'.
12+
!!! error TS2322: Type 'Symbol' is not assignable to type 'symbol'.
13+
!!! error TS2322: 'symbol' is a primitive type while 'Symbol' is a boxed object. Prefer using 'symbol' when possible.

tests/cases/compiler/nativeToBoxedTypes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ s = S;
88

99
var B = new Boolean();
1010
var b = true;
11-
b = B;
11+
b = B;
12+
13+
var sym: symbol;
14+
var Sym: Symbol;
15+
sym = Sym;

0 commit comments

Comments
 (0)