TypeScript/tests/baselines/reference/unionTypeFromArrayLiteral.types

104 lines
4.4 KiB
Plaintext
Raw Normal View History

=== 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[]
2015-04-13 23:01:57 +02:00
>arr1 : number[], Symbol(arr1, Decl(unionTypeFromArrayLiteral.ts, 5, 3))
>[1, 2] : number[]
2015-04-13 21:36:11 +02:00
>1 : number
>2 : number
var arr2 = ["hello", true]; // (string | number)[]
2015-04-13 23:01:57 +02:00
>arr2 : (string | boolean)[], Symbol(arr2, Decl(unionTypeFromArrayLiteral.ts, 6, 3))
>["hello", true] : (string | boolean)[]
2015-04-13 21:36:11 +02:00
>"hello" : string
>true : boolean
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
2015-04-13 23:01:57 +02:00
>arr3Tuple : [number, string], Symbol(arr3Tuple, Decl(unionTypeFromArrayLiteral.ts, 7, 3))
>[3, "three"] : [number, string]
2015-04-13 21:36:11 +02:00
>3 : number
>"three" : string
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
2015-04-13 23:01:57 +02:00
>arr4Tuple : [number, string], Symbol(arr4Tuple, Decl(unionTypeFromArrayLiteral.ts, 8, 3))
>[3, "three", "hello"] : [number, string, string]
2015-04-13 21:36:11 +02:00
>3 : number
>"three" : string
>"hello" : string
var arrEmpty = [];
2015-04-13 23:01:57 +02:00
>arrEmpty : any[], Symbol(arrEmpty, Decl(unionTypeFromArrayLiteral.ts, 9, 3))
>[] : undefined[]
var arr5Tuple: {
2015-04-13 23:01:57 +02:00
>arr5Tuple : { 0: string; 5: number; }, Symbol(arr5Tuple, Decl(unionTypeFromArrayLiteral.ts, 10, 3))
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]
2015-04-13 21:36:11 +02:00
>"hello" : string
>true : boolean
>false : boolean
>" hello" : string
>true : boolean
>10 : number
>"any" : string
class C { foo() { } }
2015-04-13 23:01:57 +02:00
>C : C, Symbol(C, Decl(unionTypeFromArrayLiteral.ts, 13, 54))
>foo : () => void, Symbol(foo, Decl(unionTypeFromArrayLiteral.ts, 14, 9))
class D { foo2() { } }
2015-04-13 23:01:57 +02:00
>D : D, Symbol(D, Decl(unionTypeFromArrayLiteral.ts, 14, 21))
>foo2 : () => void, Symbol(foo2, Decl(unionTypeFromArrayLiteral.ts, 15, 9))
class E extends C { foo3() { } }
2015-04-13 23:01:57 +02:00
>E : E, Symbol(E, Decl(unionTypeFromArrayLiteral.ts, 15, 22))
>C : C, Symbol(C, Decl(unionTypeFromArrayLiteral.ts, 13, 54))
>foo3 : () => void, Symbol(foo3, Decl(unionTypeFromArrayLiteral.ts, 16, 19))
class F extends C { foo4() { } }
2015-04-13 23:01:57 +02:00
>F : F, Symbol(F, Decl(unionTypeFromArrayLiteral.ts, 16, 32))
>C : C, Symbol(C, Decl(unionTypeFromArrayLiteral.ts, 13, 54))
>foo4 : () => void, Symbol(foo4, Decl(unionTypeFromArrayLiteral.ts, 17, 19))
var c: C, d: D, e: E, f: F;
2015-04-13 23:01:57 +02:00
>c : C, Symbol(c, Decl(unionTypeFromArrayLiteral.ts, 18, 3))
>C : C, Symbol(C, Decl(unionTypeFromArrayLiteral.ts, 13, 54))
>d : D, Symbol(d, Decl(unionTypeFromArrayLiteral.ts, 18, 9))
>D : D, Symbol(D, Decl(unionTypeFromArrayLiteral.ts, 14, 21))
>e : E, Symbol(e, Decl(unionTypeFromArrayLiteral.ts, 18, 15))
>E : E, Symbol(E, Decl(unionTypeFromArrayLiteral.ts, 15, 22))
>f : F, Symbol(f, Decl(unionTypeFromArrayLiteral.ts, 18, 21))
>F : F, Symbol(F, Decl(unionTypeFromArrayLiteral.ts, 16, 32))
var arr6 = [c, d]; // (C | D)[]
2015-04-13 23:01:57 +02:00
>arr6 : (C | D)[], Symbol(arr6, Decl(unionTypeFromArrayLiteral.ts, 19, 3))
>[c, d] : (C | D)[]
2015-04-13 23:01:57 +02:00
>c : C, Symbol(c, Decl(unionTypeFromArrayLiteral.ts, 18, 3))
>d : D, Symbol(d, Decl(unionTypeFromArrayLiteral.ts, 18, 9))
var arr7 = [c, d, e]; // (C | D)[]
2015-04-13 23:01:57 +02:00
>arr7 : (C | D)[], Symbol(arr7, Decl(unionTypeFromArrayLiteral.ts, 20, 3))
>[c, d, e] : (C | D)[]
2015-04-13 23:01:57 +02:00
>c : C, Symbol(c, Decl(unionTypeFromArrayLiteral.ts, 18, 3))
>d : D, Symbol(d, Decl(unionTypeFromArrayLiteral.ts, 18, 9))
>e : E, Symbol(e, Decl(unionTypeFromArrayLiteral.ts, 18, 15))
var arr8 = [c, e]; // C[]
2015-04-13 23:01:57 +02:00
>arr8 : C[], Symbol(arr8, Decl(unionTypeFromArrayLiteral.ts, 21, 3))
>[c, e] : C[]
2015-04-13 23:01:57 +02:00
>c : C, Symbol(c, Decl(unionTypeFromArrayLiteral.ts, 18, 3))
>e : E, Symbol(e, Decl(unionTypeFromArrayLiteral.ts, 18, 15))
var arr9 = [e, f]; // (E|F)[]
2015-04-13 23:01:57 +02:00
>arr9 : (E | F)[], Symbol(arr9, Decl(unionTypeFromArrayLiteral.ts, 22, 3))
>[e, f] : (E | F)[]
2015-04-13 23:01:57 +02:00
>e : E, Symbol(e, Decl(unionTypeFromArrayLiteral.ts, 18, 15))
>f : F, Symbol(f, Decl(unionTypeFromArrayLiteral.ts, 18, 21))