You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve the error message when asserting to a type that is not
comparable to the original.
Also improve the error message for implicit conversion of a symbol to a
string in a template literal, which previously shared the error message
with type assertions.
Fixesmicrosoft#25539. Addresses microsoft#25870.
Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+5-1Lines changed: 5 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1196,7 +1196,7 @@
1196
1196
"category": "Error",
1197
1197
"code": 2351
1198
1198
},
1199
-
"Type '{0}' cannot be converted to type '{1}'.": {
1199
+
"Conversion of type '{0}' to type '{1}' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.": {
1200
1200
"category": "Error",
1201
1201
"code": 2352
1202
1202
},
@@ -2421,6 +2421,10 @@
2421
2421
"category": "Error",
2422
2422
"code": 2730
2423
2423
},
2424
+
"Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.": {
2425
+
"category": "Error",
2426
+
"code": 2731
2427
+
},
2424
2428
2425
2429
"Import declaration '{0}' is using private name '{1}'.": {
Copy file name to clipboardExpand all lines: tests/baselines/reference/arrayCast.errors.txt
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
tests/cases/compiler/arrayCast.ts(3,23): error TS2352: Type '{ foo: string; }[]' cannot be converted to type '{ id: number; }[]'.
1
+
tests/cases/compiler/arrayCast.ts(3,23): error TS2352: Conversion of type '{ foo: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2
2
Type '{ foo: string; }' is not comparable to type '{ id: number; }'.
3
3
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
// has type { foo: string }[], which is not assignable to { id: number }[].
9
9
<{ id: number; }[]>[{ foo: "s" }];
10
10
~~~~~~~~
11
-
!!! error TS2352: Type '{ foo: string; }[]' cannot be converted to type '{ id: number; }[]'.
11
+
!!! error TS2352: Conversion of type '{ foo: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
12
12
!!! error TS2352: Type '{ foo: string; }' is not comparable to type '{ id: number; }'.
13
13
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
tests/cases/conformance/expressions/asOperator/asOperator2.ts(1,9): error TS2352: Type 'number' cannot be converted to type 'string'.
1
+
tests/cases/conformance/expressions/asOperator/asOperator2.ts(1,9): error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type 'number' cannot be converted to type 'string'.
7
+
!!! error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
tests/cases/conformance/expressions/asOperator/asOperatorContextualType.ts(2,9): error TS2352: Type '(v: number) => number' cannot be converted to type '(x: number) => string'.
1
+
tests/cases/conformance/expressions/asOperator/asOperatorContextualType.ts(2,9): error TS2352: Conversion of type '(v: number) => number' to type '(x: number) => string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type '(v: number) => number' cannot be converted to type '(x: number) => string'.
9
+
!!! error TS2352: Conversion of type '(v: number) => number' to type '(x: number) => string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
10
10
!!! error TS2352: Type 'number' is not comparable to type 'string'.
tests/cases/conformance/expressions/asOperator/asOperatorNames.ts(2,9): error TS2352: Type 'number' cannot be converted to type 'string'.
1
+
tests/cases/conformance/expressions/asOperator/asOperatorNames.ts(2,9): error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type 'number' cannot be converted to type 'string'.
8
+
!!! error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Copy file name to clipboardExpand all lines: tests/baselines/reference/castingTuple.errors.txt
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
tests/cases/conformance/types/tuple/castingTuple.ts(13,23): error TS2352: Type '[number, string]' cannot be converted to type '[number, string, boolean]'.
1
+
tests/cases/conformance/types/tuple/castingTuple.ts(13,23): error TS2352: Conversion of type '[number, string]' to type '[number, string, boolean]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2
2
Property '2' is missing in type '[number, string]'.
3
-
tests/cases/conformance/types/tuple/castingTuple.ts(14,15): error TS2352: Type '[number, string, boolean]' cannot be converted to type '[number, string]'.
3
+
tests/cases/conformance/types/tuple/castingTuple.ts(14,15): error TS2352: Conversion of type '[number, string, boolean]' to type '[number, string]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
4
4
Types of property 'length' are incompatible.
5
5
Type '3' is not comparable to type '2'.
6
-
tests/cases/conformance/types/tuple/castingTuple.ts(15,14): error TS2352: Type '[number, string]' cannot be converted to type '[number, string, boolean]'.
7
-
tests/cases/conformance/types/tuple/castingTuple.ts(18,21): error TS2352: Type '[C, D]' cannot be converted to type '[C, D, A]'.
6
+
tests/cases/conformance/types/tuple/castingTuple.ts(15,14): error TS2352: Conversion of type '[number, string]' to type '[number, string, boolean]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
7
+
tests/cases/conformance/types/tuple/castingTuple.ts(18,21): error TS2352: Conversion of type '[C, D]' to type '[C, D, A]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
8
8
Property '2' is missing in type '[C, D]'.
9
-
tests/cases/conformance/types/tuple/castingTuple.ts(30,10): error TS2352: Type '[number, string]' cannot be converted to type '[number, number]'.
9
+
tests/cases/conformance/types/tuple/castingTuple.ts(30,10): error TS2352: Conversion of type '[number, string]' to type '[number, number]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
10
10
Type 'string' is not comparable to type 'number'.
11
-
tests/cases/conformance/types/tuple/castingTuple.ts(31,10): error TS2352: Type '[C, D]' cannot be converted to type '[A, I]'.
11
+
tests/cases/conformance/types/tuple/castingTuple.ts(31,10): error TS2352: Conversion of type '[C, D]' to type '[A, I]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
12
12
Type 'C' is not comparable to type 'A'.
13
13
Property 'a' is missing in type 'C'.
14
14
tests/cases/conformance/types/tuple/castingTuple.ts(32,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'array1' must be of type '{}[]', but here has type 'number[]'.
var numStrBoolTuple = <[number, string, boolean]>numStrTuple;
32
32
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
-
!!! error TS2352: Type '[number, string]' cannot be converted to type '[number, string, boolean]'.
33
+
!!! error TS2352: Conversion of type '[number, string]' to type '[number, string, boolean]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
34
34
!!! error TS2352: Property '2' is missing in type '[number, string]'.
35
35
var shorter = numStrBoolTuple as [number, string]
36
36
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
-
!!! error TS2352: Type '[number, string, boolean]' cannot be converted to type '[number, string]'.
37
+
!!! error TS2352: Conversion of type '[number, string, boolean]' to type '[number, string]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
38
38
!!! error TS2352: Types of property 'length' are incompatible.
39
39
!!! error TS2352: Type '3' is not comparable to type '2'.
40
40
var longer = numStrTuple as [number, string, boolean]
41
41
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
-
!!! error TS2352: Type '[number, string]' cannot be converted to type '[number, string, boolean]'.
42
+
!!! error TS2352: Conversion of type '[number, string]' to type '[number, string, boolean]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
43
43
var classCDTuple: [C, D] = [new C(), new D()];
44
44
var interfaceIITuple = <[I, I]>classCDTuple;
45
45
var classCDATuple = <[C, D, A]>classCDTuple;
46
46
~~~~~~~~~~~~~~~~~~~~~~~
47
-
!!! error TS2352: Type '[C, D]' cannot be converted to type '[C, D, A]'.
47
+
!!! error TS2352: Conversion of type '[C, D]' to type '[C, D, A]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
48
48
!!! error TS2352: Property '2' is missing in type '[C, D]'.
!!! error TS2352: Type '[number, string]' cannot be converted to type '[number, number]'.
62
+
!!! error TS2352: Conversion of type '[number, string]' to type '[number, number]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
63
63
!!! error TS2352: Type 'string' is not comparable to type 'number'.
64
64
var t9 = <[A, I]>classCDTuple;
65
65
~~~~~~~~~~~~~~~~~~~~
66
-
!!! error TS2352: Type '[C, D]' cannot be converted to type '[A, I]'.
66
+
!!! error TS2352: Conversion of type '[C, D]' to type '[A, I]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
67
67
!!! error TS2352: Type 'C' is not comparable to type 'A'.
68
68
!!! error TS2352: Property 'a' is missing in type 'C'.
tests/cases/compiler/contextualTyping39.ts(1,11): error TS2352: Type '() => string' cannot be converted to type '() => number'.
1
+
tests/cases/compiler/contextualTyping39.ts(1,11): error TS2352: Conversion of type '() => string' to type '() => number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type '() => string' cannot be converted to type '() => number'.
8
+
!!! error TS2352: Conversion of type '() => string' to type '() => number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
9
9
!!! error TS2352: Type 'string' is not comparable to type 'number'.
tests/cases/compiler/contextualTyping41.ts(1,11): error TS2352: Type '() => string' cannot be converted to type '{ (): number; (i: number): number; }'.
1
+
tests/cases/compiler/contextualTyping41.ts(1,11): error TS2352: Conversion of type '() => string' to type '{ (): number; (i: number): number; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type '() => string' cannot be converted to type '{ (): number; (i: number): number; }'.
8
+
!!! error TS2352: Conversion of type '() => string' to type '{ (): number; (i: number): number; }' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
9
9
!!! error TS2352: Type 'string' is not comparable to type 'number'.
Copy file name to clipboardExpand all lines: tests/baselines/reference/defaultArgsInFunctionExpressions.errors.txt
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@ tests/cases/compiler/defaultArgsInFunctionExpressions.ts(4,19): error TS2345: Ar
2
2
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(5,1): error TS2322: Type 'number' is not assignable to type 'string'.
3
3
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(8,20): error TS2322: Type '3' is not assignable to type 'string'.
4
4
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(11,1): error TS2322: Type 'string' is not assignable to type 'number'.
5
-
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(14,51): error TS2352: Type 'string' cannot be converted to type 'number'.
5
+
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(14,51): error TS2352: Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
6
6
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(17,41): error TS2322: Type '""' is not assignable to type 'number'.
7
-
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(20,62): error TS2352: Type 'string' cannot be converted to type 'number'.
7
+
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(20,62): error TS2352: Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
8
8
tests/cases/compiler/defaultArgsInFunctionExpressions.ts(28,15): error TS2708: Cannot use namespace 'T' as a value.
9
9
10
10
@@ -32,7 +32,7 @@ tests/cases/compiler/defaultArgsInFunctionExpressions.ts(28,15): error TS2708: C
32
32
// Contextually type the default arg with the type annotation
33
33
var f3 = function (a: (s: string) => any = (s) => <number>s) { };
34
34
~~~~~~~~~
35
-
!!! error TS2352: Type 'string' cannot be converted to type 'number'.
35
+
!!! error TS2352: Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
36
36
37
37
// Type check using the function's contextual type
38
38
var f4: (a: number) => void = function (a = "") { };
@@ -42,7 +42,7 @@ tests/cases/compiler/defaultArgsInFunctionExpressions.ts(28,15): error TS2708: C
42
42
// Contextually type the default arg using the function's contextual type
43
43
var f5: (a: (s: string) => any) => void = function (a = s => <number>s) { };
44
44
~~~~~~~~~
45
-
!!! error TS2352: Type 'string' cannot be converted to type 'number'.
45
+
!!! error TS2352: Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
tests/cases/compiler/fuzzy.ts(21,34): error TS2322: Type 'this' is not assignable to type 'I'.
4
4
Type 'C' is not assignable to type 'I'.
5
-
tests/cases/compiler/fuzzy.ts(25,20): error TS2352: Type '{ oneI: this; }' cannot be converted to type 'R'.
5
+
tests/cases/compiler/fuzzy.ts(25,20): error TS2352: Conversion of type '{ oneI: this; }' to type 'R' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
6
6
Property 'anything' is missing in type '{ oneI: this; }'.
!!! error TS2352: Type '{ oneI: this; }' cannot be converted to type 'R'.
43
+
!!! error TS2352: Conversion of type '{ oneI: this; }' to type 'R' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
44
44
!!! error TS2352: Property 'anything' is missing in type '{ oneI: this; }'.
0 commit comments