|
| 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)[] |
0 commit comments