Skip to content

Commit b14b231

Browse files
Report primitive type in literal-to-primitive relation complaints
1 parent b346f57 commit b14b231

File tree

242 files changed

+1099
-1104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

242 files changed

+1099
-1104
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4070,12 +4070,19 @@ namespace ts {
40704070
let leftStr = symbolValueDeclarationIsContextSensitive(left.symbol) ? typeToString(left, left.symbol.valueDeclaration) : typeToString(left);
40714071
let rightStr = symbolValueDeclarationIsContextSensitive(right.symbol) ? typeToString(right, right.symbol.valueDeclaration) : typeToString(right);
40724072
if (leftStr === rightStr) {
4073-
leftStr = typeToString(left, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
4074-
rightStr = typeToString(right, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
4073+
leftStr = getTypeNameForErrorDisplay(left);
4074+
rightStr = getTypeNameForErrorDisplay(right);
4075+
}
4076+
else if (isLiteralType(left) && !isLiteralType(right)) {
4077+
leftStr = getTypeNameForErrorDisplay(getBaseTypeOfLiteralType(left))
40754078
}
40764079
return [leftStr, rightStr];
40774080
}
40784081

4082+
function getTypeNameForErrorDisplay(type: Type) {
4083+
return typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
4084+
}
4085+
40794086
function symbolValueDeclarationIsContextSensitive(symbol: Symbol): boolean {
40804087
return symbol && symbol.valueDeclaration && isExpression(symbol.valueDeclaration) && !isContextSensitive(symbol.valueDeclaration);
40814088
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts(3,6): error TS2461: Type 'string | number' is not an array type.
2-
tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts(3,7): error TS2322: Type '1' is not assignable to type 'string'.
3-
tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts(3,14): error TS2322: Type '""' is not assignable to type 'number'.
2+
tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'.
3+
tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts(3,14): error TS2322: Type 'string' is not assignable to type 'number'.
44

55

66
==== tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts (3 errors) ====
@@ -10,9 +10,9 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-of30.ts(3,14): error
1010
~~~~~~~~~~~~~~~
1111
!!! error TS2461: Type 'string | number' is not an array type.
1212
~
13-
!!! error TS2322: Type '1' is not assignable to type 'string'.
13+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
1414
~
15-
!!! error TS2322: Type '""' is not assignable to type 'number'.
15+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
1616
a;
1717
b;
1818
}

tests/baselines/reference/accessors_spec_section-4.5_error-cases.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(2,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
22
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(3,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
3-
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(3,48): error TS2322: Type '""' is not assignable to type 'number'.
3+
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(3,48): error TS2322: Type 'string' is not assignable to type 'number'.
44
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(5,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
5-
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(5,47): error TS2322: Type '""' is not assignable to type 'number'.
5+
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(5,47): error TS2322: Type 'string' is not assignable to type 'number'.
66
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(6,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
77
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(8,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
88
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(9,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
9-
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(9,52): error TS2322: Type '0' is not assignable to type 'string'.
9+
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(9,52): error TS2322: Type 'number' is not assignable to type 'string'.
1010
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(11,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
11-
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(11,51): error TS2322: Type '0' is not assignable to type 'string'.
11+
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(11,51): error TS2322: Type 'number' is not assignable to type 'string'.
1212
tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(12,16): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
1313

1414

@@ -21,13 +21,13 @@ tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(12,16): error TS1
2121
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2222
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2323
~~~~~~~~~~
24-
!!! error TS2322: Type '""' is not assignable to type 'number'.
24+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
2525

2626
public get AnnotatedSetter_SetterLast() { return ""; }
2727
~~~~~~~~~~~~~~~~~~~~~~~~~~
2828
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2929
~~~~~~~~~~
30-
!!! error TS2322: Type '""' is not assignable to type 'number'.
30+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
3131
public set AnnotatedSetter_SetterLast(a: number) { }
3232
~~~~~~~~~~~~~~~~~~~~~~~~~~
3333
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
@@ -39,13 +39,13 @@ tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(12,16): error TS1
3939
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4040
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
4141
~~~~
42-
!!! error TS2322: Type '0' is not assignable to type 'string'.
42+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
4343

4444
public set AnnotatedGetter_GetterLast(aStr) { aStr = 0; }
4545
~~~~~~~~~~~~~~~~~~~~~~~~~~
4646
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
4747
~~~~
48-
!!! error TS2322: Type '0' is not assignable to type 'string'.
48+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
4949
public get AnnotatedGetter_GetterLast(): string { return ""; }
5050
~~~~~~~~~~~~~~~~~~~~~~~~~~
5151
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.

tests/baselines/reference/aliasAssignments.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/aliasAssignments_1.ts(3,1): error TS2322: Type '1' is not assignable to type 'typeof import("tests/cases/compiler/aliasAssignments_moduleA")'.
1+
tests/cases/compiler/aliasAssignments_1.ts(3,1): error TS2322: Type 'number' is not assignable to type 'typeof import("tests/cases/compiler/aliasAssignments_moduleA")'.
22
tests/cases/compiler/aliasAssignments_1.ts(5,1): error TS2322: Type 'typeof import("tests/cases/compiler/aliasAssignments_moduleA")' is not assignable to type 'number'.
33

44

@@ -7,7 +7,7 @@ tests/cases/compiler/aliasAssignments_1.ts(5,1): error TS2322: Type 'typeof impo
77
var x = moduleA;
88
x = 1; // Should be error
99
~
10-
!!! error TS2322: Type '1' is not assignable to type 'typeof import("tests/cases/compiler/aliasAssignments_moduleA")'.
10+
!!! error TS2322: Type 'number' is not assignable to type 'typeof import("tests/cases/compiler/aliasAssignments_moduleA")'.
1111
var y = 1;
1212
y = moduleA; // should be error
1313
~

tests/baselines/reference/allowJscheckJsTypeParameterNoCrash.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/app.js(6,7): error TS2322: Type '1' is not assignable to type 'WatchHandler<any>'.
1+
tests/cases/compiler/app.js(6,7): error TS2322: Type 'number' is not assignable to type 'WatchHandler<any>'.
22

33

44
==== tests/cases/compiler/func.ts (0 errors) ====
@@ -16,7 +16,7 @@ tests/cases/compiler/app.js(6,7): error TS2322: Type '1' is not assignable to ty
1616
data1(val) {
1717
this.data2 = 1;
1818
~~~~~~~~~~
19-
!!! error TS2322: Type '1' is not assignable to type 'WatchHandler<any>'.
19+
!!! error TS2322: Type 'number' is not assignable to type 'WatchHandler<any>'.
2020
},
2121
data2(val) { },
2222
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/argumentsBindsToFunctionScopeArgumentList.ts(3,5): error TS2322: Type '10' is not assignable to type 'IArguments'.
1+
tests/cases/compiler/argumentsBindsToFunctionScopeArgumentList.ts(3,5): error TS2322: Type 'number' is not assignable to type 'IArguments'.
22

33

44
==== tests/cases/compiler/argumentsBindsToFunctionScopeArgumentList.ts (1 errors) ====
55
var arguments = 10;
66
function foo(a) {
77
arguments = 10; /// This shouldnt be of type number and result in error.
88
~~~~~~~~~
9-
!!! error TS2322: Type '10' is not assignable to type 'IArguments'.
9+
!!! error TS2322: Type 'number' is not assignable to type 'IArguments'.
1010
}

tests/baselines/reference/arrayLiterals3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(10,5): error TS2739: Type '[]' is missing the following properties from type '[any, any, any]': 0, 1, 2
22
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,38): error TS2322: Type 'string' is not assignable to type 'boolean'.
33
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,48): error TS2322: Type 'number' is not assignable to type 'string'.
4-
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,51): error TS2322: Type 'true' is not assignable to type 'number'.
4+
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(11,51): error TS2322: Type 'boolean' is not assignable to type 'number'.
55
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(17,5): error TS2322: Type '[number, number, string, boolean]' is not assignable to type '[number, number]'.
66
Types of property 'length' are incompatible.
77
Type '4' is not assignable to type '2'.
@@ -31,7 +31,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
3131
~
3232
!!! error TS2322: Type 'number' is not assignable to type 'string'.
3333
~~~~
34-
!!! error TS2322: Type 'true' is not assignable to type 'number'.
34+
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
3535

3636
// The resulting type an array literal expression is determined as follows:
3737
// - If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.17.1),

tests/baselines/reference/assignToFn.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/assignToFn.ts(8,5): error TS2322: Type '"hello"' is not assignable to type '(n: number) => boolean'.
1+
tests/cases/compiler/assignToFn.ts(8,5): error TS2322: Type 'string' is not assignable to type '(n: number) => boolean'.
22

33

44
==== tests/cases/compiler/assignToFn.ts (1 errors) ====
@@ -11,6 +11,6 @@ tests/cases/compiler/assignToFn.ts(8,5): error TS2322: Type '"hello"' is not ass
1111

1212
x.f="hello";
1313
~~~
14-
!!! error TS2322: Type '"hello"' is not assignable to type '(n: number) => boolean'.
14+
!!! error TS2322: Type 'string' is not assignable to type '(n: number) => boolean'.
1515
}
1616

tests/baselines/reference/assignmentCompat1.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/compiler/assignmentCompat1.ts(4,1): error TS2741: Property 'one' is missing in type '{ [index: string]: any; }' but required in type '{ one: number; }'.
22
tests/cases/compiler/assignmentCompat1.ts(6,1): error TS2741: Property 'one' is missing in type '{ [index: number]: any; }' but required in type '{ one: number; }'.
3-
tests/cases/compiler/assignmentCompat1.ts(8,1): error TS2322: Type '"foo"' is not assignable to type '{ [index: string]: any; }'.
4-
tests/cases/compiler/assignmentCompat1.ts(10,1): error TS2322: Type 'false' is not assignable to type '{ [index: number]: any; }'.
3+
tests/cases/compiler/assignmentCompat1.ts(8,1): error TS2322: Type 'string' is not assignable to type '{ [index: string]: any; }'.
4+
tests/cases/compiler/assignmentCompat1.ts(10,1): error TS2322: Type 'boolean' is not assignable to type '{ [index: number]: any; }'.
55

66

77
==== tests/cases/compiler/assignmentCompat1.ts (4 errors) ====
@@ -20,10 +20,10 @@ tests/cases/compiler/assignmentCompat1.ts(10,1): error TS2322: Type 'false' is n
2020
z = x; // Ok because index signature type is any
2121
y = "foo"; // Error
2222
~
23-
!!! error TS2322: Type '"foo"' is not assignable to type '{ [index: string]: any; }'.
23+
!!! error TS2322: Type 'string' is not assignable to type '{ [index: string]: any; }'.
2424
z = "foo"; // OK, string has numeric indexer
2525
z = false; // Error
2626
~
27-
!!! error TS2322: Type 'false' is not assignable to type '{ [index: number]: any; }'.
27+
!!! error TS2322: Type 'boolean' is not assignable to type '{ [index: number]: any; }'.
2828

2929

tests/baselines/reference/assignmentCompatFunctionsWithOptionalArgs.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(1,10): error TS2391: Function implementation is missing or not immediately following the declaration.
2-
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(4,17): error TS2322: Type 'false' is not assignable to type 'string'.
2+
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(4,17): error TS2322: Type 'boolean' is not assignable to type 'string'.
33
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(5,5): error TS2345: Argument of type '{ name: string; }' is not assignable to parameter of type '{ id: number; name?: string; }'.
44
Property 'id' is missing in type '{ name: string; }' but required in type '{ id: number; name?: string; }'.
55

@@ -12,7 +12,7 @@ tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(5,5): error TS
1212
foo({ id: 1234, name: "hello" }); // Ok
1313
foo({ id: 1234, name: false }); // Error, name of wrong type
1414
~~~~
15-
!!! error TS2322: Type 'false' is not assignable to type 'string'.
15+
!!! error TS2322: Type 'boolean' is not assignable to type 'string'.
1616
!!! related TS6500 tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts:1:31: The expected type comes from property 'name' which is declared here on type '{ id: number; name?: string; }'
1717
foo({ name: "hello" }); // Error, id required but missing
1818
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)