Skip to content

Commit 40af15c

Browse files
committed
Array literal produces union type if the it is not contextually typed
1 parent 1ca9273 commit 40af15c

3 files changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//// [unionTypeFromArrayLiteral.ts]
2+
// The resulting type an array literal expression is determined as follows:
3+
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
4+
// Otherwise, if the array literal is contextually typed by a type that has a property with the numeric name ‘0’, the resulting type is a tuple type constructed from the types of the element expressions.
5+
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
6+
7+
var arr1 = [1, 2]; // number[]
8+
var arr2 = ["hello", true]; // (string | number)[]
9+
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
10+
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
11+
var arrEmpty = [];
12+
var arr5Tuple: {
13+
0: string;
14+
5: number;
15+
} = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
16+
class C { foo() { } }
17+
class D { foo2() { } }
18+
class E extends C { foo3() { } }
19+
class F extends C { foo4() { } }
20+
var c: C, d: D, e: E, f: F;
21+
var arr6 = [c, d]; // (C | D)[]
22+
var arr7 = [c, d, e]; // (C | D)[]
23+
var arr8 = [c, e]; // C[]
24+
var arr9 = [e, f]; // (E|F)[]
25+
26+
//// [unionTypeFromArrayLiteral.js]
27+
// The resulting type an array literal expression is determined as follows:
28+
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
29+
// Otherwise, if the array literal is contextually typed by a type that has a property with the numeric name ‘0’, the resulting type is a tuple type constructed from the types of the element expressions.
30+
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
31+
var __extends = this.__extends || function (d, b) {
32+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
33+
function __() { this.constructor = d; }
34+
__.prototype = b.prototype;
35+
d.prototype = new __();
36+
};
37+
var arr1 = [1, 2]; // number[]
38+
var arr2 = ["hello", true]; // (string | number)[]
39+
var arr3Tuple = [3, "three"]; // [number, string]
40+
var arr4Tuple = [3, "three", "hello"]; // [number, string, string]
41+
var arrEmpty = [];
42+
var arr5Tuple = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
43+
var C = (function () {
44+
function C() {
45+
}
46+
C.prototype.foo = function () {
47+
};
48+
return C;
49+
})();
50+
var D = (function () {
51+
function D() {
52+
}
53+
D.prototype.foo2 = function () {
54+
};
55+
return D;
56+
})();
57+
var E = (function (_super) {
58+
__extends(E, _super);
59+
function E() {
60+
_super.apply(this, arguments);
61+
}
62+
E.prototype.foo3 = function () {
63+
};
64+
return E;
65+
})(C);
66+
var F = (function (_super) {
67+
__extends(F, _super);
68+
function F() {
69+
_super.apply(this, arguments);
70+
}
71+
F.prototype.foo4 = function () {
72+
};
73+
return F;
74+
})(C);
75+
var c, d, e, f;
76+
var arr6 = [c, d]; // (C | D)[]
77+
var arr7 = [c, d, e]; // (C | D)[]
78+
var arr8 = [c, e]; // C[]
79+
var arr9 = [e, f]; // (E|F)[]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
=== tests/cases/conformance/types/union/unionTypeFromArrayLiteral.ts ===
2+
// The resulting type an array literal expression is determined as follows:
3+
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
4+
// Otherwise, if the array literal is contextually typed by a type that has a property with the numeric name ‘0’, the resulting type is a tuple type constructed from the types of the element expressions.
5+
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
6+
7+
var arr1 = [1, 2]; // number[]
8+
>arr1 : number[]
9+
>[1, 2] : number[]
10+
11+
var arr2 = ["hello", true]; // (string | number)[]
12+
>arr2 : (string | boolean)[]
13+
>["hello", true] : (string | boolean)[]
14+
15+
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
16+
>arr3Tuple : [number, string]
17+
>[3, "three"] : [number, string]
18+
19+
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
20+
>arr4Tuple : [number, string]
21+
>[3, "three", "hello"] : [number, string, string]
22+
23+
var arrEmpty = [];
24+
>arrEmpty : any[]
25+
>[] : undefined[]
26+
27+
var arr5Tuple: {
28+
>arr5Tuple : { 0: string; 5: number; }
29+
30+
0: string;
31+
5: number;
32+
} = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
33+
>["hello", true, false, " hello", true, 10, "any"] : [string, boolean, boolean, string, boolean, number, string]
34+
35+
class C { foo() { } }
36+
>C : C
37+
>foo : () => void
38+
39+
class D { foo2() { } }
40+
>D : D
41+
>foo2 : () => void
42+
43+
class E extends C { foo3() { } }
44+
>E : E
45+
>C : C
46+
>foo3 : () => void
47+
48+
class F extends C { foo4() { } }
49+
>F : F
50+
>C : C
51+
>foo4 : () => void
52+
53+
var c: C, d: D, e: E, f: F;
54+
>c : C
55+
>C : C
56+
>d : D
57+
>D : D
58+
>e : E
59+
>E : E
60+
>f : F
61+
>F : F
62+
63+
var arr6 = [c, d]; // (C | D)[]
64+
>arr6 : (C | D)[]
65+
>[c, d] : (C | D)[]
66+
>c : C
67+
>d : D
68+
69+
var arr7 = [c, d, e]; // (C | D)[]
70+
>arr7 : (C | D)[]
71+
>[c, d, e] : (C | D)[]
72+
>c : C
73+
>d : D
74+
>e : E
75+
76+
var arr8 = [c, e]; // C[]
77+
>arr8 : C[]
78+
>[c, e] : C[]
79+
>c : C
80+
>e : E
81+
82+
var arr9 = [e, f]; // (E|F)[]
83+
>arr9 : (E | F)[]
84+
>[e, f] : (E | F)[]
85+
>e : E
86+
>f : F
87+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// The resulting type an array literal expression is determined as follows:
2+
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
3+
// Otherwise, if the array literal is contextually typed by a type that has a property with the numeric name ‘0’, the resulting type is a tuple type constructed from the types of the element expressions.
4+
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
5+
6+
var arr1 = [1, 2]; // number[]
7+
var arr2 = ["hello", true]; // (string | number)[]
8+
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
9+
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
10+
var arrEmpty = [];
11+
var arr5Tuple: {
12+
0: string;
13+
5: number;
14+
} = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
15+
class C { foo() { } }
16+
class D { foo2() { } }
17+
class E extends C { foo3() { } }
18+
class F extends C { foo4() { } }
19+
var c: C, d: D, e: E, f: F;
20+
var arr6 = [c, d]; // (C | D)[]
21+
var arr7 = [c, d, e]; // (C | D)[]
22+
var arr8 = [c, e]; // C[]
23+
var arr9 = [e, f]; // (E|F)[]

0 commit comments

Comments
 (0)