Skip to content

Commit 7634f74

Browse files
committed
Conformance tests for array element type of tuple is union type of tuple element types
1 parent 2b70196 commit 7634f74

9 files changed

Lines changed: 251 additions & 35 deletions

File tree

tests/baselines/reference/castingTuple.errors.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ tests/cases/conformance/types/tuple/castingTuple.ts(13,23): error TS2352: Neithe
22
Property '2' is missing in type '[number, string]'.
33
tests/cases/conformance/types/tuple/castingTuple.ts(16,21): error TS2352: Neither type '[C, D]' nor type '[C, D, A]' is assignable to the other.
44
Property '2' is missing in type '[C, D]'.
5-
tests/cases/conformance/types/tuple/castingTuple.ts(24,10): error TS2352: Neither type '[number, string]' nor type '[number, number]' is assignable to the other.
5+
tests/cases/conformance/types/tuple/castingTuple.ts(28,10): error TS2352: Neither type '[number, string]' nor type '[number, number]' is assignable to the other.
66
Types of property '1' are incompatible.
77
Type 'string' is not assignable to type 'number'.
8-
tests/cases/conformance/types/tuple/castingTuple.ts(25,10): error TS2352: Neither type '[C, D]' nor type '[A, I]' is assignable to the other.
8+
tests/cases/conformance/types/tuple/castingTuple.ts(29,10): error TS2352: Neither type '[C, D]' nor type '[A, I]' is assignable to the other.
99
Types of property '0' are incompatible.
1010
Type 'C' is not assignable to type 'A'.
11-
tests/cases/conformance/types/tuple/castingTuple.ts(26,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'array1' must be of type '{}[]', but here has type 'number[]'.
12-
tests/cases/conformance/types/tuple/castingTuple.ts(26,14): error TS2352: Neither type '[number, string]' nor type 'number[]' is assignable to the other.
11+
tests/cases/conformance/types/tuple/castingTuple.ts(30,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'array1' must be of type '{}[]', but here has type 'number[]'.
12+
tests/cases/conformance/types/tuple/castingTuple.ts(30,14): error TS2352: Neither type '[number, string]' nor type 'number[]' is assignable to the other.
1313
Types of property 'pop' are incompatible.
1414
Type '() => string | number' is not assignable to type '() => number'.
1515
Type 'string | number' is not assignable to type 'number'.
1616
Type 'string' is not assignable to type 'number'.
17-
tests/cases/conformance/types/tuple/castingTuple.ts(27,1): error TS2304: Cannot find name 't4'.
17+
tests/cases/conformance/types/tuple/castingTuple.ts(31,1): error TS2304: Cannot find name 't4'.
1818

1919

2020
==== tests/cases/conformance/types/tuple/castingTuple.ts (7 errors) ====
@@ -41,10 +41,14 @@ tests/cases/conformance/types/tuple/castingTuple.ts(27,1): error TS2304: Cannot
4141
!!! error TS2352: Neither type '[C, D]' nor type '[C, D, A]' is assignable to the other.
4242
!!! error TS2352: Property '2' is missing in type '[C, D]'.
4343
var eleFromCDA1 = classCDATuple[2]; // A
44-
var eleFromCDA2 = classCDATuple[5]; // {}
44+
var eleFromCDA2 = classCDATuple[5]; // C | D | A
4545
var t10: [E1, E2] = [E1.one, E2.one];
4646
var t11 = <[number, number]>t10;
4747
var array1 = <{}[]>emptyObjTuple;
48+
var unionTuple: [C, string | number] = [new C(), "foo"];
49+
var unionTuple2: [C, string | number, D] = [new C(), "foo", new D()];
50+
var unionTuple3: [number, string| number] = [10, "foo"];
51+
var unionTuple4 = <[number, number]>unionTuple3;
4852

4953
// error
5054
var t3 = <[number, number]>numStrTuple;
@@ -68,4 +72,5 @@ tests/cases/conformance/types/tuple/castingTuple.ts(27,1): error TS2304: Cannot
6872
!!! error TS2352: Type 'string' is not assignable to type 'number'.
6973
t4[2] = 10;
7074
~~
71-
!!! error TS2304: Cannot find name 't4'.
75+
!!! error TS2304: Cannot find name 't4'.
76+

tests/baselines/reference/castingTuple.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@ var classCDTuple: [C, D] = [new C(), new D()];
1616
var interfaceIITuple = <[I, I]>classCDTuple;
1717
var classCDATuple = <[C, D, A]>classCDTuple;
1818
var eleFromCDA1 = classCDATuple[2]; // A
19-
var eleFromCDA2 = classCDATuple[5]; // {}
19+
var eleFromCDA2 = classCDATuple[5]; // C | D | A
2020
var t10: [E1, E2] = [E1.one, E2.one];
2121
var t11 = <[number, number]>t10;
2222
var array1 = <{}[]>emptyObjTuple;
23+
var unionTuple: [C, string | number] = [new C(), "foo"];
24+
var unionTuple2: [C, string | number, D] = [new C(), "foo", new D()];
25+
var unionTuple3: [number, string| number] = [10, "foo"];
26+
var unionTuple4 = <[number, number]>unionTuple3;
2327

2428
// error
2529
var t3 = <[number, number]>numStrTuple;
2630
var t9 = <[A, I]>classCDTuple;
2731
var array1 = <number[]>numStrTuple;
28-
t4[2] = 10;
32+
t4[2] = 10;
33+
2934

3035
//// [castingTuple.js]
3136
var __extends = this.__extends || function (d, b) {
@@ -84,10 +89,14 @@ var classCDTuple = [new C(), new D()];
8489
var interfaceIITuple = classCDTuple;
8590
var classCDATuple = classCDTuple;
8691
var eleFromCDA1 = classCDATuple[2]; // A
87-
var eleFromCDA2 = classCDATuple[5]; // {}
92+
var eleFromCDA2 = classCDATuple[5]; // C | D | A
8893
var t10 = [0 /* one */, 0 /* one */];
8994
var t11 = t10;
9095
var array1 = emptyObjTuple;
96+
var unionTuple = [new C(), "foo"];
97+
var unionTuple2 = [new C(), "foo", new D()];
98+
var unionTuple3 = [10, "foo"];
99+
var unionTuple4 = unionTuple3;
91100
// error
92101
var t3 = numStrTuple;
93102
var t9 = classCDTuple;

tests/baselines/reference/contextualTypeWithTuple.errors.txt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(3,5): error TS232
44
Type 'string | number | boolean' is not assignable to type 'string | number'.
55
Type 'boolean' is not assignable to type 'string | number'.
66
Type 'boolean' is not assignable to type 'number'.
7-
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(8,1): error TS2323: Type '[number, string, boolean]' is not assignable to type '[number, string]'.
8-
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(11,1): error TS2323: Type '[{}, number]' is not assignable to type '[{ a: string; }, number]'.
7+
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(15,1): error TS2323: Type '[number, string, boolean]' is not assignable to type '[number, string]'.
8+
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(18,1): error TS2323: Type '[{}, number]' is not assignable to type '[{ a: string; }, number]'.
99
Types of property '0' are incompatible.
1010
Type '{}' is not assignable to type '{ a: string; }'.
1111
Property 'a' is missing in type '{}'.
12-
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(12,1): error TS2323: Type '[number, string]' is not assignable to type '[number, string, boolean]'.
12+
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(19,1): error TS2323: Type '[number, string]' is not assignable to type '[number, string, boolean]'.
1313
Property '2' is missing in type '[number, string]'.
14-
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(13,5): error TS2323: Type '[string, string, number]' is not assignable to type '[string, string]'.
14+
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(20,5): error TS2323: Type '[string, string, number]' is not assignable to type '[string, string]'.
1515
Types of property 'pop' are incompatible.
1616
Type '() => string | number' is not assignable to type '() => string'.
1717
Type 'string | number' is not assignable to type 'string'.
1818
Type 'number' is not assignable to type 'string'.
19+
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(24,1): error TS2323: Type '[C, string | number]' is not assignable to type '[C, string | number, D]'.
20+
Property '2' is missing in type '[C, string | number]'.
21+
tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(25,1): error TS2323: Type '[number, string | number]' is not assignable to type '[number, string]'.
22+
Types of property '1' are incompatible.
23+
Type 'string | number' is not assignable to type 'string'.
24+
Type 'number' is not assignable to type 'string'.
1925

2026

21-
==== tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts (5 errors) ====
27+
==== tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts (7 errors) ====
2228
// no error
2329
var numStrTuple: [number, string] = [5, "hello"];
2430
var numStrTuple2: [number, string] = [5, "foo", true];
@@ -32,6 +38,13 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(13,5): error TS23
3238
var numStrBoolTuple: [number, string, boolean] = [5, "foo", true];
3339
var objNumTuple: [{ a: string }, number] = [{ a: "world" }, 5];
3440
var strTupleTuple: [string, [number, {}]] = ["bar", [5, { x: 1, y: 1 }]];
41+
class C { }
42+
class D { }
43+
var unionTuple: [C, string | number] = [new C(), "foo"];
44+
var unionTuple1: [C, string | number] = [new C(), "foo"];
45+
var unionTuple2: [C, string | number, D] = [new C(), "foo", new D()];
46+
var unionTuple3: [number, string| number] = [10, "foo"];
47+
3548
numStrTuple = numStrTuple2;
3649
numStrTuple = numStrBoolTuple;
3750
~~~~~~~~~~~
@@ -55,4 +68,16 @@ tests/cases/conformance/types/tuple/contextualTypeWithTuple.ts(13,5): error TS23
5568
!!! error TS2323: Type '() => string | number' is not assignable to type '() => string'.
5669
!!! error TS2323: Type 'string | number' is not assignable to type 'string'.
5770
!!! error TS2323: Type 'number' is not assignable to type 'string'.
58-
71+
72+
unionTuple = unionTuple1;
73+
unionTuple = unionTuple2;
74+
unionTuple2 = unionTuple;
75+
~~~~~~~~~~~
76+
!!! error TS2323: Type '[C, string | number]' is not assignable to type '[C, string | number, D]'.
77+
!!! error TS2323: Property '2' is missing in type '[C, string | number]'.
78+
numStrTuple = unionTuple3;
79+
~~~~~~~~~~~
80+
!!! error TS2323: Type '[number, string | number]' is not assignable to type '[number, string]'.
81+
!!! error TS2323: Types of property '1' are incompatible.
82+
!!! error TS2323: Type 'string | number' is not assignable to type 'string'.
83+
!!! error TS2323: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/contextualTypeWithTuple.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@ var numStrTuple2: [number, string] = [5, "foo", true];
55
var numStrBoolTuple: [number, string, boolean] = [5, "foo", true];
66
var objNumTuple: [{ a: string }, number] = [{ a: "world" }, 5];
77
var strTupleTuple: [string, [number, {}]] = ["bar", [5, { x: 1, y: 1 }]];
8+
class C { }
9+
class D { }
10+
var unionTuple: [C, string | number] = [new C(), "foo"];
11+
var unionTuple1: [C, string | number] = [new C(), "foo"];
12+
var unionTuple2: [C, string | number, D] = [new C(), "foo", new D()];
13+
var unionTuple3: [number, string| number] = [10, "foo"];
14+
815
numStrTuple = numStrTuple2;
916
numStrTuple = numStrBoolTuple;
1017

1118
// error
1219
objNumTuple = [ {}, 5];
1320
numStrBoolTuple = numStrTuple;
1421
var strStrTuple: [string, string] = ["foo", "bar", 5];
15-
22+
23+
unionTuple = unionTuple1;
24+
unionTuple = unionTuple2;
25+
unionTuple2 = unionTuple;
26+
numStrTuple = unionTuple3;
1627

1728
//// [contextualTypeWithTuple.js]
1829
// no error
@@ -21,9 +32,27 @@ var numStrTuple2 = [5, "foo", true];
2132
var numStrBoolTuple = [5, "foo", true];
2233
var objNumTuple = [{ a: "world" }, 5];
2334
var strTupleTuple = ["bar", [5, { x: 1, y: 1 }]];
35+
var C = (function () {
36+
function C() {
37+
}
38+
return C;
39+
})();
40+
var D = (function () {
41+
function D() {
42+
}
43+
return D;
44+
})();
45+
var unionTuple = [new C(), "foo"];
46+
var unionTuple1 = [new C(), "foo"];
47+
var unionTuple2 = [new C(), "foo", new D()];
48+
var unionTuple3 = [10, "foo"];
2449
numStrTuple = numStrTuple2;
2550
numStrTuple = numStrBoolTuple;
2651
// error
2752
objNumTuple = [{}, 5];
2853
numStrBoolTuple = numStrTuple;
2954
var strStrTuple = ["foo", "bar", 5];
55+
unionTuple = unionTuple1;
56+
unionTuple = unionTuple2;
57+
unionTuple2 = unionTuple;
58+
numStrTuple = unionTuple3;
Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,65 @@
11
//// [indexerWithTuple.ts]
22
var strNumTuple: [string, number] = ["foo", 10];
33
var numTupleTuple: [number, [string, number]] = [10, ["bar", 20]];
4+
var unionTuple1: [number, string| number] = [10, "foo"];
5+
var unionTuple2: [boolean, string| number] = [true, "foo"];
46

57
// no error
68
var idx0 = 0;
79
var idx1 = 1;
810
var ele10 = strNumTuple[0]; // string
911
var ele11 = strNumTuple[1]; // number
10-
var ele12 = strNumTuple[2]; // {}
11-
var ele13 = strNumTuple[idx0]; // {}
12-
var ele14 = strNumTuple[idx1]; // {}
12+
var ele12 = strNumTuple[2]; // string | number
13+
var ele13 = strNumTuple[idx0]; // string | number
14+
var ele14 = strNumTuple[idx1]; // string | number
1315
var ele15 = strNumTuple["0"]; // string
1416
var ele16 = strNumTuple["1"]; // number
1517
var strNumTuple1 = numTupleTuple[1]; //[string, number];
16-
var ele17 = numTupleTuple[2]; // {}
18+
var ele17 = numTupleTuple[2]; // number | [string, number]
19+
var eleUnion10 = unionTuple1[0]; // number
20+
var eleUnion11 = unionTuple1[1]; // string | number
21+
var eleUnion12 = unionTuple1[2]; // string | number
22+
var eleUnion13 = unionTuple1[idx0]; // string | number
23+
var eleUnion14 = unionTuple1[idx1]; // string | number
24+
var eleUnion15 = unionTuple1["0"]; // number
25+
var eleUnion16 = unionTuple1["1"]; // string | number
26+
27+
var eleUnion20 = unionTuple2[0]; // boolean
28+
var eleUnion21 = unionTuple2[1]; // string | number
29+
var eleUnion22 = unionTuple2[2]; // string | number | boolean
30+
var eleUnion23 = unionTuple2[idx0]; // string | number | boolean
31+
var eleUnion24 = unionTuple2[idx1]; // string | number | boolean
32+
var eleUnion25 = unionTuple2["0"]; // boolean
33+
var eleUnion26 = unionTuple2["1"]; // string | number
1734

1835
//// [indexerWithTuple.js]
1936
var strNumTuple = ["foo", 10];
2037
var numTupleTuple = [10, ["bar", 20]];
38+
var unionTuple1 = [10, "foo"];
39+
var unionTuple2 = [true, "foo"];
2140
// no error
2241
var idx0 = 0;
2342
var idx1 = 1;
2443
var ele10 = strNumTuple[0]; // string
2544
var ele11 = strNumTuple[1]; // number
26-
var ele12 = strNumTuple[2]; // {}
27-
var ele13 = strNumTuple[idx0]; // {}
28-
var ele14 = strNumTuple[idx1]; // {}
45+
var ele12 = strNumTuple[2]; // string | number
46+
var ele13 = strNumTuple[idx0]; // string | number
47+
var ele14 = strNumTuple[idx1]; // string | number
2948
var ele15 = strNumTuple["0"]; // string
3049
var ele16 = strNumTuple["1"]; // number
3150
var strNumTuple1 = numTupleTuple[1]; //[string, number];
32-
var ele17 = numTupleTuple[2]; // {}
51+
var ele17 = numTupleTuple[2]; // number | [string, number]
52+
var eleUnion10 = unionTuple1[0]; // number
53+
var eleUnion11 = unionTuple1[1]; // string | number
54+
var eleUnion12 = unionTuple1[2]; // string | number
55+
var eleUnion13 = unionTuple1[idx0]; // string | number
56+
var eleUnion14 = unionTuple1[idx1]; // string | number
57+
var eleUnion15 = unionTuple1["0"]; // number
58+
var eleUnion16 = unionTuple1["1"]; // string | number
59+
var eleUnion20 = unionTuple2[0]; // boolean
60+
var eleUnion21 = unionTuple2[1]; // string | number
61+
var eleUnion22 = unionTuple2[2]; // string | number | boolean
62+
var eleUnion23 = unionTuple2[idx0]; // string | number | boolean
63+
var eleUnion24 = unionTuple2[idx1]; // string | number | boolean
64+
var eleUnion25 = unionTuple2["0"]; // boolean
65+
var eleUnion26 = unionTuple2["1"]; // string | number

0 commit comments

Comments
 (0)