-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy patharrayLiterals3.types
More file actions
142 lines (128 loc) · 4.23 KB
/
arrayLiterals3.types
File metadata and controls
142 lines (128 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//// [tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts] ////
=== arrayLiterals3.ts ===
// Each element expression in a non-empty array literal is processed as follows:
// - If the array literal contains no spread elements, and if the array literal is contextually typed (section 4.19)
// by a type T and T has a property with the numeric name N, where N is the index of the element expression in the array literal,
// the element expression is contextually typed by the type of that property.
// The resulting type an array literal expression is determined as follows:
// - If the array literal contains no spread elements and is contextually typed by a tuple-like type,
// the resulting type is a tuple type constructed from the types of the element expressions.
var a0: [any, any, any] = []; // Error
>a0 : [any, any, any]
> : ^^^^^^^^^^^^^^^
>[] : []
> : ^^
var a1: [boolean, string, number] = ["string", 1, true]; // Error
>a1 : [boolean, string, number]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>["string", 1, true] : [string, number, boolean]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>"string" : "string"
> : ^^^^^^^^
>1 : 1
> : ^
>true : true
> : ^^^^
// The resulting type an array literal expression is determined as follows:
// - If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.17.1),
// the resulting type is a tuple type constructed from the types of the element expressions.
var [b1, b2]: [number, number] = [1, 2, "string", true];
>b1 : number
> : ^^^^^^
>b2 : number
> : ^^^^^^
>[1, 2, "string", true] : [number, number, string, boolean]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>1 : 1
> : ^
>2 : 2
> : ^
>"string" : "string"
> : ^^^^^^^^
>true : true
> : ^^^^
// The resulting type an array literal expression is determined as follows:
// - the resulting type is an array type with an element type that is the union of the types of the
// non - spread element expressions and the numeric index signature types of the spread element expressions
var temp = ["s", "t", "r"];
>temp : string[]
> : ^^^^^^^^
>["s", "t", "r"] : string[]
> : ^^^^^^^^
>"s" : "s"
> : ^^^
>"t" : "t"
> : ^^^
>"r" : "r"
> : ^^^
var temp1 = [1, 2, 3];
>temp1 : number[]
> : ^^^^^^^^
>[1, 2, 3] : number[]
> : ^^^^^^^^
>1 : 1
> : ^
>2 : 2
> : ^
>3 : 3
> : ^
var temp2: [number[], string[]] = [[1, 2, 3], ["hello", "string"]];
>temp2 : [number[], string[]]
> : ^^^^^^^^^^^^^^^^^^^^
>[[1, 2, 3], ["hello", "string"]] : [number[], string[]]
> : ^^^^^^^^^^^^^^^^^^^^
>[1, 2, 3] : number[]
> : ^^^^^^^^
>1 : 1
> : ^
>2 : 2
> : ^
>3 : 3
> : ^
>["hello", "string"] : string[]
> : ^^^^^^^^
>"hello" : "hello"
> : ^^^^^^^
>"string" : "string"
> : ^^^^^^^^
interface tup {
0: number[]|string[];
>0 : string[] | number[]
> : ^^^^^^^^^^^^^^^^^^^
1: number[]|string[];
>1 : string[] | number[]
> : ^^^^^^^^^^^^^^^^^^^
}
interface myArray extends Array<Number> { }
interface myArray2 extends Array<Number|String> { }
var c0: tup = [...temp2]; // Error
>c0 : tup
> : ^^^
>[...temp2] : [number[], string[]]
> : ^^^^^^^^^^^^^^^^^^^^
>...temp2 : string[] | number[]
> : ^^^^^^^^^^^^^^^^^^^
>temp2 : [number[], string[]]
> : ^^^^^^^^^^^^^^^^^^^^
var c1: [number, number, number] = [...temp1]; // Error cannot assign number[] to [number, number, number]
>c1 : [number, number, number]
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>[...temp1] : number[]
> : ^^^^^^^^
>...temp1 : number
> : ^^^^^^
>temp1 : number[]
> : ^^^^^^^^
var c2: myArray = [...temp1, ...temp]; // Error cannot assign (number|string)[] to number[]
>c2 : myArray
> : ^^^^^^^
>[...temp1, ...temp] : (string | number)[]
> : ^^^^^^^^^^^^^^^^^^^
>...temp1 : number
> : ^^^^^^
>temp1 : number[]
> : ^^^^^^^^
>...temp : string
> : ^^^^^^
>temp : string[]
> : ^^^^^^^^