Skip to content

Commit 54fa950

Browse files
IllusionMHweswigham
authored andcommitted
Skip primitive types comparison with array and object types (microsoft#31077)
1 parent b02b823 commit 54fa950

9 files changed

+222
-10
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11859,6 +11859,7 @@ namespace ts {
1185911859
}
1186011860

1186111861
function elaborateArrayLiteral(node: ArrayLiteralExpression, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
11862+
if (target.flags & TypeFlags.Primitive) return false;
1186211863
if (isTupleLikeType(source)) {
1186311864
return elaborateElementwise(generateLimitedTupleElements(node, target), source, target, relation);
1186411865
}
@@ -11895,6 +11896,7 @@ namespace ts {
1189511896
}
1189611897

1189711898
function elaborateObjectLiteral(node: ObjectLiteralExpression, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
11899+
if (target.flags & TypeFlags.Primitive) return false;
1189811900
return elaborateElementwise(generateObjectLiteralElements(node), source, target, relation);
1189911901
}
1190011902

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(1,7): error TS2322: Type 'number[]' is not assignable to type 'number'.
2+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(2,7): error TS2322: Type 'string[]' is not assignable to type 'number'.
3+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(3,7): error TS2322: Type '(string | number)[]' is not assignable to type 'number'.
4+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(4,7): error TS2322: Type 'number[]' is not assignable to type '0'.
5+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(6,7): error TS2322: Type 'number[]' is not assignable to type 'string'.
6+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(7,7): error TS2322: Type 'string[]' is not assignable to type 'string'.
7+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(8,7): error TS2322: Type '(string | number)[]' is not assignable to type 'string'.
8+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(9,7): error TS2322: Type 'string[]' is not assignable to type '"01"'.
9+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(11,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'number'.
10+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(13,7): error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
11+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(14,7): error TS2322: Type '{ "0": number; }' is not assignable to type 'string'.
12+
tests/cases/compiler/assignmentIndexedToPrimitives.ts(15,7): error TS2322: Type '{ 0: string; }' is not assignable to type 'string'.
13+
14+
15+
==== tests/cases/compiler/assignmentIndexedToPrimitives.ts (12 errors) ====
16+
const n1: number = [0];
17+
~~
18+
!!! error TS2322: Type 'number[]' is not assignable to type 'number'.
19+
const n2: number = ["0"];
20+
~~
21+
!!! error TS2322: Type 'string[]' is not assignable to type 'number'.
22+
const n3: number = [0, "1"];
23+
~~
24+
!!! error TS2322: Type '(string | number)[]' is not assignable to type 'number'.
25+
const n4: 0 = [0];
26+
~~
27+
!!! error TS2322: Type 'number[]' is not assignable to type '0'.
28+
29+
const s1: string = [0];
30+
~~
31+
!!! error TS2322: Type 'number[]' is not assignable to type 'string'.
32+
const s2: string = ["0"];
33+
~~
34+
!!! error TS2322: Type 'string[]' is not assignable to type 'string'.
35+
const s3: string = [0, "1"];
36+
~~
37+
!!! error TS2322: Type '(string | number)[]' is not assignable to type 'string'.
38+
const s4: "01" = ["0", "1"];
39+
~~
40+
!!! error TS2322: Type 'string[]' is not assignable to type '"01"'.
41+
42+
const no1: number = { 0: 1 };
43+
~~~
44+
!!! error TS2322: Type '{ 0: number; }' is not assignable to type 'number'.
45+
46+
const so1: string = { 0: 1 };
47+
~~~
48+
!!! error TS2322: Type '{ 0: number; }' is not assignable to type 'string'.
49+
const so2: string = { "0": 1 };
50+
~~~
51+
!!! error TS2322: Type '{ "0": number; }' is not assignable to type 'string'.
52+
const so3: string = { 0: "1" };
53+
~~~
54+
!!! error TS2322: Type '{ 0: string; }' is not assignable to type 'string'.
55+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [assignmentIndexedToPrimitives.ts]
2+
const n1: number = [0];
3+
const n2: number = ["0"];
4+
const n3: number = [0, "1"];
5+
const n4: 0 = [0];
6+
7+
const s1: string = [0];
8+
const s2: string = ["0"];
9+
const s3: string = [0, "1"];
10+
const s4: "01" = ["0", "1"];
11+
12+
const no1: number = { 0: 1 };
13+
14+
const so1: string = { 0: 1 };
15+
const so2: string = { "0": 1 };
16+
const so3: string = { 0: "1" };
17+
18+
19+
//// [assignmentIndexedToPrimitives.js]
20+
var n1 = [0];
21+
var n2 = ["0"];
22+
var n3 = [0, "1"];
23+
var n4 = [0];
24+
var s1 = [0];
25+
var s2 = ["0"];
26+
var s3 = [0, "1"];
27+
var s4 = ["0", "1"];
28+
var no1 = { 0: 1 };
29+
var so1 = { 0: 1 };
30+
var so2 = { "0": 1 };
31+
var so3 = { 0: "1" };
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/compiler/assignmentIndexedToPrimitives.ts ===
2+
const n1: number = [0];
3+
>n1 : Symbol(n1, Decl(assignmentIndexedToPrimitives.ts, 0, 5))
4+
5+
const n2: number = ["0"];
6+
>n2 : Symbol(n2, Decl(assignmentIndexedToPrimitives.ts, 1, 5))
7+
8+
const n3: number = [0, "1"];
9+
>n3 : Symbol(n3, Decl(assignmentIndexedToPrimitives.ts, 2, 5))
10+
11+
const n4: 0 = [0];
12+
>n4 : Symbol(n4, Decl(assignmentIndexedToPrimitives.ts, 3, 5))
13+
14+
const s1: string = [0];
15+
>s1 : Symbol(s1, Decl(assignmentIndexedToPrimitives.ts, 5, 5))
16+
17+
const s2: string = ["0"];
18+
>s2 : Symbol(s2, Decl(assignmentIndexedToPrimitives.ts, 6, 5))
19+
20+
const s3: string = [0, "1"];
21+
>s3 : Symbol(s3, Decl(assignmentIndexedToPrimitives.ts, 7, 5))
22+
23+
const s4: "01" = ["0", "1"];
24+
>s4 : Symbol(s4, Decl(assignmentIndexedToPrimitives.ts, 8, 5))
25+
26+
const no1: number = { 0: 1 };
27+
>no1 : Symbol(no1, Decl(assignmentIndexedToPrimitives.ts, 10, 5))
28+
>0 : Symbol(0, Decl(assignmentIndexedToPrimitives.ts, 10, 21))
29+
30+
const so1: string = { 0: 1 };
31+
>so1 : Symbol(so1, Decl(assignmentIndexedToPrimitives.ts, 12, 5))
32+
>0 : Symbol(0, Decl(assignmentIndexedToPrimitives.ts, 12, 21))
33+
34+
const so2: string = { "0": 1 };
35+
>so2 : Symbol(so2, Decl(assignmentIndexedToPrimitives.ts, 13, 5))
36+
>"0" : Symbol("0", Decl(assignmentIndexedToPrimitives.ts, 13, 21))
37+
38+
const so3: string = { 0: "1" };
39+
>so3 : Symbol(so3, Decl(assignmentIndexedToPrimitives.ts, 14, 5))
40+
>0 : Symbol(0, Decl(assignmentIndexedToPrimitives.ts, 14, 21))
41+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
=== tests/cases/compiler/assignmentIndexedToPrimitives.ts ===
2+
const n1: number = [0];
3+
>n1 : number
4+
>[0] : number[]
5+
>0 : 0
6+
7+
const n2: number = ["0"];
8+
>n2 : number
9+
>["0"] : string[]
10+
>"0" : "0"
11+
12+
const n3: number = [0, "1"];
13+
>n3 : number
14+
>[0, "1"] : (string | number)[]
15+
>0 : 0
16+
>"1" : "1"
17+
18+
const n4: 0 = [0];
19+
>n4 : 0
20+
>[0] : number[]
21+
>0 : 0
22+
23+
const s1: string = [0];
24+
>s1 : string
25+
>[0] : number[]
26+
>0 : 0
27+
28+
const s2: string = ["0"];
29+
>s2 : string
30+
>["0"] : string[]
31+
>"0" : "0"
32+
33+
const s3: string = [0, "1"];
34+
>s3 : string
35+
>[0, "1"] : (string | number)[]
36+
>0 : 0
37+
>"1" : "1"
38+
39+
const s4: "01" = ["0", "1"];
40+
>s4 : "01"
41+
>["0", "1"] : string[]
42+
>"0" : "0"
43+
>"1" : "1"
44+
45+
const no1: number = { 0: 1 };
46+
>no1 : number
47+
>{ 0: 1 } : { 0: number; }
48+
>0 : number
49+
>1 : 1
50+
51+
const so1: string = { 0: 1 };
52+
>so1 : string
53+
>{ 0: 1 } : { 0: number; }
54+
>0 : number
55+
>1 : 1
56+
57+
const so2: string = { "0": 1 };
58+
>so2 : string
59+
>{ "0": 1 } : { "0": number; }
60+
>"0" : number
61+
>1 : 1
62+
63+
const so3: string = { 0: "1" };
64+
>so3 : string
65+
>{ 0: "1" } : { 0: string; }
66+
>0 : string
67+
>"1" : "1"
68+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/es6/for-ofStatements/for-of10.ts(2,12): error TS2322: Type 'number' is not assignable to type 'string'.
1+
tests/cases/conformance/es6/for-ofStatements/for-of10.ts(2,6): error TS2322: Type 'number' is not assignable to type 'string'.
22

33

44
==== tests/cases/conformance/es6/for-ofStatements/for-of10.ts (1 errors) ====
55
var v: string;
66
for (v of [0]) { }
7-
~
7+
~
88
!!! error TS2322: Type 'number' is not assignable to type 'string'.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
tests/cases/conformance/es6/for-ofStatements/for-of11.ts(2,12): error TS2322: Type 'number' is not assignable to type 'string'.
1+
tests/cases/conformance/es6/for-ofStatements/for-of11.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
2+
Type 'number' is not assignable to type 'string'.
23

34

45
==== tests/cases/conformance/es6/for-ofStatements/for-of11.ts (1 errors) ====
56
var v: string;
67
for (v of [0, ""]) { }
7-
~
8-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
8+
~
9+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
10+
!!! error TS2322: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/wrappedAndRecursiveConstraints4.errors.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursiveConstraints4.ts(13,25): error TS2322: Type '(x: number) => void' is not assignable to type '(pos: number) => string'.
2-
Type 'void' is not assignable to type 'string'.
1+
tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursiveConstraints4.ts(13,12): error TS2345: Argument of type '{ length: number; charAt: (x: number) => void; }' is not assignable to parameter of type 'string'.
32

43

54
==== tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursiveConstraints4.ts (1 errors) ====
@@ -16,6 +15,5 @@ tests/cases/conformance/types/typeParameters/typeArgumentLists/wrappedAndRecursi
1615
var c = new C({ length: 2 });
1716
var r = c.foo('');
1817
var r2 = r({ length: 3, charAt: (x: number) => { '' } }); // error
19-
~~~~~~
20-
!!! error TS2322: Type '(x: number) => void' is not assignable to type '(pos: number) => string'.
21-
!!! error TS2322: Type 'void' is not assignable to type 'string'.
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS2345: Argument of type '{ length: number; charAt: (x: number) => void; }' is not assignable to parameter of type 'string'.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const n1: number = [0];
2+
const n2: number = ["0"];
3+
const n3: number = [0, "1"];
4+
const n4: 0 = [0];
5+
6+
const s1: string = [0];
7+
const s2: string = ["0"];
8+
const s3: string = [0, "1"];
9+
const s4: "01" = ["0", "1"];
10+
11+
const no1: number = { 0: 1 };
12+
13+
const so1: string = { 0: 1 };
14+
const so2: string = { "0": 1 };
15+
const so3: string = { 0: "1" };

0 commit comments

Comments
 (0)