2014-11-05 00:58:44 +01:00
=== 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-16 01:44:20 +02:00
>arr1 : number[]
2014-11-05 00:58:44 +01:00
>[1, 2] : number[]
2015-04-13 21:36:11 +02:00
>1 : number
>2 : number
2014-11-05 00:58:44 +01:00
var arr2 = ["hello", true]; // (string | number)[]
2015-04-16 01:44:20 +02:00
>arr2 : (string | boolean)[]
2014-11-05 00:58:44 +01:00
>["hello", true] : (string | boolean)[]
2015-04-13 21:36:11 +02:00
>"hello" : string
>true : boolean
2014-11-05 00:58:44 +01:00
var arr3Tuple: [number, string] = [3, "three"]; // [number, string]
2015-04-16 01:44:20 +02:00
>arr3Tuple : [number, string]
2014-11-05 00:58:44 +01:00
>[3, "three"] : [number, string]
2015-04-13 21:36:11 +02:00
>3 : number
>"three" : string
2014-11-05 00:58:44 +01:00
var arr4Tuple: [number, string] = [3, "three", "hello"]; // [number, string, string]
2015-04-16 01:44:20 +02:00
>arr4Tuple : [number, string]
2014-11-05 00:58:44 +01:00
>[3, "three", "hello"] : [number, string, string]
2015-04-13 21:36:11 +02:00
>3 : number
>"three" : string
>"hello" : string
2014-11-05 00:58:44 +01:00
var arrEmpty = [];
2015-04-16 01:44:20 +02:00
>arrEmpty : any[]
2014-11-05 00:58:44 +01:00
>[] : undefined[]
var arr5Tuple: {
2015-04-16 01:44:20 +02:00
>arr5Tuple : { 0: string; 5: number; }
2014-11-05 00:58:44 +01:00
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
2014-11-05 00:58:44 +01:00
class C { foo() { } }
2015-04-16 01:44:20 +02:00
>C : C
>foo : () => void
2014-11-05 00:58:44 +01:00
class D { foo2() { } }
2015-04-16 01:44:20 +02:00
>D : D
>foo2 : () => void
2014-11-05 00:58:44 +01:00
class E extends C { foo3() { } }
2015-04-16 01:44:20 +02:00
>E : E
>C : C
>foo3 : () => void
2014-11-05 00:58:44 +01:00
class F extends C { foo4() { } }
2015-04-16 01:44:20 +02:00
>F : F
>C : C
>foo4 : () => void
2014-11-05 00:58:44 +01:00
var c: C, d: D, e: E, f: F;
2015-04-16 01:44:20 +02:00
>c : C
>C : C
>d : D
>D : D
>e : E
>E : E
>f : F
>F : F
2014-11-05 00:58:44 +01:00
var arr6 = [c, d]; // (C | D)[]
2015-04-16 01:44:20 +02:00
>arr6 : (C | D)[]
2014-11-05 00:58:44 +01:00
>[c, d] : (C | D)[]
2015-04-16 01:44:20 +02:00
>c : C
>d : D
2014-11-05 00:58:44 +01:00
var arr7 = [c, d, e]; // (C | D)[]
2015-04-16 01:44:20 +02:00
>arr7 : (C | D)[]
2014-11-05 00:58:44 +01:00
>[c, d, e] : (C | D)[]
2015-04-16 01:44:20 +02:00
>c : C
>d : D
>e : E
2014-11-05 00:58:44 +01:00
var arr8 = [c, e]; // C[]
2015-04-16 01:44:20 +02:00
>arr8 : C[]
2014-11-05 00:58:44 +01:00
>[c, e] : C[]
2015-04-16 01:44:20 +02:00
>c : C
>e : E
2014-11-05 00:58:44 +01:00
var arr9 = [e, f]; // (E|F)[]
2015-04-16 01:44:20 +02:00
>arr9 : (E | F)[]
2014-11-05 00:58:44 +01:00
>[e, f] : (E | F)[]
2015-04-16 01:44:20 +02:00
>e : E
>f : F
2014-11-05 00:58:44 +01:00