mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
Array literal produces union type if the it is not contextually typed
This commit is contained in:
parent
1ca9273f49
commit
40af15c8ff
79
tests/baselines/reference/unionTypeFromArrayLiteral.js
Normal file
79
tests/baselines/reference/unionTypeFromArrayLiteral.js
Normal file
@ -0,0 +1,79 @@
|
||||
//// [unionTypeFromArrayLiteral.ts]
|
||||
// The resulting type an array literal expression is determined as follows:
|
||||
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
|
||||
// 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.
|
||||
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
|
||||
|
||||
var arr1 = [1, 2]; // number[]
|
||||
var arr2 = ["hello", true]; // (string | number)[]
|
||||
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
|
||||
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
|
||||
var arrEmpty = [];
|
||||
var arr5Tuple: {
|
||||
0: string;
|
||||
5: number;
|
||||
} = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
|
||||
class C { foo() { } }
|
||||
class D { foo2() { } }
|
||||
class E extends C { foo3() { } }
|
||||
class F extends C { foo4() { } }
|
||||
var c: C, d: D, e: E, f: F;
|
||||
var arr6 = [c, d]; // (C | D)[]
|
||||
var arr7 = [c, d, e]; // (C | D)[]
|
||||
var arr8 = [c, e]; // C[]
|
||||
var arr9 = [e, f]; // (E|F)[]
|
||||
|
||||
//// [unionTypeFromArrayLiteral.js]
|
||||
// The resulting type an array literal expression is determined as follows:
|
||||
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
|
||||
// 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.
|
||||
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
|
||||
var __extends = this.__extends || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
__.prototype = b.prototype;
|
||||
d.prototype = new __();
|
||||
};
|
||||
var arr1 = [1, 2]; // number[]
|
||||
var arr2 = ["hello", true]; // (string | number)[]
|
||||
var arr3Tuple = [3, "three"]; // [number, string]
|
||||
var arr4Tuple = [3, "three", "hello"]; // [number, string, string]
|
||||
var arrEmpty = [];
|
||||
var arr5Tuple = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
|
||||
var C = (function () {
|
||||
function C() {
|
||||
}
|
||||
C.prototype.foo = function () {
|
||||
};
|
||||
return C;
|
||||
})();
|
||||
var D = (function () {
|
||||
function D() {
|
||||
}
|
||||
D.prototype.foo2 = function () {
|
||||
};
|
||||
return D;
|
||||
})();
|
||||
var E = (function (_super) {
|
||||
__extends(E, _super);
|
||||
function E() {
|
||||
_super.apply(this, arguments);
|
||||
}
|
||||
E.prototype.foo3 = function () {
|
||||
};
|
||||
return E;
|
||||
})(C);
|
||||
var F = (function (_super) {
|
||||
__extends(F, _super);
|
||||
function F() {
|
||||
_super.apply(this, arguments);
|
||||
}
|
||||
F.prototype.foo4 = function () {
|
||||
};
|
||||
return F;
|
||||
})(C);
|
||||
var c, d, e, f;
|
||||
var arr6 = [c, d]; // (C | D)[]
|
||||
var arr7 = [c, d, e]; // (C | D)[]
|
||||
var arr8 = [c, e]; // C[]
|
||||
var arr9 = [e, f]; // (E|F)[]
|
||||
87
tests/baselines/reference/unionTypeFromArrayLiteral.types
Normal file
87
tests/baselines/reference/unionTypeFromArrayLiteral.types
Normal file
@ -0,0 +1,87 @@
|
||||
=== tests/cases/conformance/types/union/unionTypeFromArrayLiteral.ts ===
|
||||
// The resulting type an array literal expression is determined as follows:
|
||||
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
|
||||
// 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.
|
||||
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
|
||||
|
||||
var arr1 = [1, 2]; // number[]
|
||||
>arr1 : number[]
|
||||
>[1, 2] : number[]
|
||||
|
||||
var arr2 = ["hello", true]; // (string | number)[]
|
||||
>arr2 : (string | boolean)[]
|
||||
>["hello", true] : (string | boolean)[]
|
||||
|
||||
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
|
||||
>arr3Tuple : [number, string]
|
||||
>[3, "three"] : [number, string]
|
||||
|
||||
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
|
||||
>arr4Tuple : [number, string]
|
||||
>[3, "three", "hello"] : [number, string, string]
|
||||
|
||||
var arrEmpty = [];
|
||||
>arrEmpty : any[]
|
||||
>[] : undefined[]
|
||||
|
||||
var arr5Tuple: {
|
||||
>arr5Tuple : { 0: string; 5: number; }
|
||||
|
||||
0: string;
|
||||
5: number;
|
||||
} = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
|
||||
>["hello", true, false, " hello", true, 10, "any"] : [string, boolean, boolean, string, boolean, number, string]
|
||||
|
||||
class C { foo() { } }
|
||||
>C : C
|
||||
>foo : () => void
|
||||
|
||||
class D { foo2() { } }
|
||||
>D : D
|
||||
>foo2 : () => void
|
||||
|
||||
class E extends C { foo3() { } }
|
||||
>E : E
|
||||
>C : C
|
||||
>foo3 : () => void
|
||||
|
||||
class F extends C { foo4() { } }
|
||||
>F : F
|
||||
>C : C
|
||||
>foo4 : () => void
|
||||
|
||||
var c: C, d: D, e: E, f: F;
|
||||
>c : C
|
||||
>C : C
|
||||
>d : D
|
||||
>D : D
|
||||
>e : E
|
||||
>E : E
|
||||
>f : F
|
||||
>F : F
|
||||
|
||||
var arr6 = [c, d]; // (C | D)[]
|
||||
>arr6 : (C | D)[]
|
||||
>[c, d] : (C | D)[]
|
||||
>c : C
|
||||
>d : D
|
||||
|
||||
var arr7 = [c, d, e]; // (C | D)[]
|
||||
>arr7 : (C | D)[]
|
||||
>[c, d, e] : (C | D)[]
|
||||
>c : C
|
||||
>d : D
|
||||
>e : E
|
||||
|
||||
var arr8 = [c, e]; // C[]
|
||||
>arr8 : C[]
|
||||
>[c, e] : C[]
|
||||
>c : C
|
||||
>e : E
|
||||
|
||||
var arr9 = [e, f]; // (E|F)[]
|
||||
>arr9 : (E | F)[]
|
||||
>[e, f] : (E | F)[]
|
||||
>e : E
|
||||
>f : F
|
||||
|
||||
@ -0,0 +1,23 @@
|
||||
// The resulting type an array literal expression is determined as follows:
|
||||
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
|
||||
// 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.
|
||||
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
|
||||
|
||||
var arr1 = [1, 2]; // number[]
|
||||
var arr2 = ["hello", true]; // (string | number)[]
|
||||
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
|
||||
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
|
||||
var arrEmpty = [];
|
||||
var arr5Tuple: {
|
||||
0: string;
|
||||
5: number;
|
||||
} = ["hello", true, false, " hello", true, 10, "any"]; // Tuple
|
||||
class C { foo() { } }
|
||||
class D { foo2() { } }
|
||||
class E extends C { foo3() { } }
|
||||
class F extends C { foo4() { } }
|
||||
var c: C, d: D, e: E, f: F;
|
||||
var arr6 = [c, d]; // (C | D)[]
|
||||
var arr7 = [c, d, e]; // (C | D)[]
|
||||
var arr8 = [c, e]; // C[]
|
||||
var arr9 = [e, f]; // (E|F)[]
|
||||
Loading…
x
Reference in New Issue
Block a user