forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputedPropertyNamesUnionTypes.symbols
More file actions
216 lines (177 loc) · 10.4 KB
/
computedPropertyNamesUnionTypes.symbols
File metadata and controls
216 lines (177 loc) · 10.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//// [tests/cases/compiler/computedPropertyNamesUnionTypes.ts] ////
=== computedPropertyNamesUnionTypes.ts ===
// Fixes #13948: Computed property key names should not be widened when the key
// type is a union of literal types.
interface Person {
>Person : Symbol(Person, Decl(computedPropertyNamesUnionTypes.ts, 0, 0))
name: string;
>name : Symbol(Person.name, Decl(computedPropertyNamesUnionTypes.ts, 3, 18))
age: number;
>age : Symbol(Person.age, Decl(computedPropertyNamesUnionTypes.ts, 4, 17))
}
// Union of string literal keys should produce distributed named properties
function unionStringLiterals(key: 'a' | 'b', value: number) {
>unionStringLiterals : Symbol(unionStringLiterals, Decl(computedPropertyNamesUnionTypes.ts, 6, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 9, 29))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 9, 44))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 10, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 10, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 9, 29))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 9, 44))
obj.a; // ok
>obj.a : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 10, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 10, 9))
>a : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 10, 17))
obj.b; // ok
>obj.b : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 10, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 10, 9))
>b : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 10, 17))
}
// keyof should work with computed properties
function keyofComputed(key: keyof Person, value: string | number) {
>keyofComputed : Symbol(keyofComputed, Decl(computedPropertyNamesUnionTypes.ts, 13, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 16, 23))
>Person : Symbol(Person, Decl(computedPropertyNamesUnionTypes.ts, 0, 0))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 16, 41))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 17, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 17, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 16, 23))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 16, 41))
obj.name; // ok
>obj.name : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 17, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 17, 9))
>name : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 17, 17))
obj.age; // ok
>obj.age : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 17, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 17, 9))
>age : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 17, 17))
}
// Partial<T> assignability (React setState pattern)
declare function setState(state: Partial<Person>): void;
>setState : Symbol(setState, Decl(computedPropertyNamesUnionTypes.ts, 20, 1))
>state : Symbol(state, Decl(computedPropertyNamesUnionTypes.ts, 23, 26))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>Person : Symbol(Person, Decl(computedPropertyNamesUnionTypes.ts, 0, 0))
function reactSetState(key: 'name', value: string) {
>reactSetState : Symbol(reactSetState, Decl(computedPropertyNamesUnionTypes.ts, 23, 56))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 24, 23))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 24, 35))
setState({ [key]: value }); // should not error
>setState : Symbol(setState, Decl(computedPropertyNamesUnionTypes.ts, 20, 1))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 25, 14))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 24, 23))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 24, 35))
}
// Three-member union
function threeWay(key: 'x' | 'y' | 'z', value: boolean) {
>threeWay : Symbol(threeWay, Decl(computedPropertyNamesUnionTypes.ts, 26, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 29, 18))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 29, 39))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 30, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 29, 18))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 29, 39))
obj.x; // ok
>obj.x : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 30, 9))
>x : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
obj.y; // ok
>obj.y : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 30, 9))
>y : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
obj.z; // ok
>obj.z : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 30, 9))
>z : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 30, 17))
}
// Number literal union
function numberLiterals(key: 0 | 1, value: string) {
>numberLiterals : Symbol(numberLiterals, Decl(computedPropertyNamesUnionTypes.ts, 34, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 37, 24))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 37, 35))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 38, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 38, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 37, 24))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 37, 35))
}
// Union key + fixed properties
function withFixed(key: 'a' | 'b') {
>withFixed : Symbol(withFixed, Decl(computedPropertyNamesUnionTypes.ts, 39, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 42, 19))
const obj = { [key]: 1, fixed: 'hello' };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 43, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 43, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 42, 19))
>fixed : Symbol(fixed, Decl(computedPropertyNamesUnionTypes.ts, 43, 27))
obj.a; // ok, number
>obj.a : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 43, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 43, 9))
>a : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 43, 17))
obj.b; // ok, number
>obj.b : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 43, 17))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 43, 9))
>b : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 43, 17))
obj.fixed; // ok, string
>obj.fixed : Symbol(fixed, Decl(computedPropertyNamesUnionTypes.ts, 43, 27))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 43, 9))
>fixed : Symbol(fixed, Decl(computedPropertyNamesUnionTypes.ts, 43, 27))
}
// Mapped type equivalence
type Mapped = { [P in 'x' | 'y']: boolean };
>Mapped : Symbol(Mapped, Decl(computedPropertyNamesUnionTypes.ts, 47, 1))
>P : Symbol(P, Decl(computedPropertyNamesUnionTypes.ts, 50, 17))
function mappedEquivalence(key: 'x' | 'y', value: boolean) {
>mappedEquivalence : Symbol(mappedEquivalence, Decl(computedPropertyNamesUnionTypes.ts, 50, 44))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 51, 27))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 51, 42))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 52, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 52, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 51, 27))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 51, 42))
const mapped: Mapped = obj; // should be assignable
>mapped : Symbol(mapped, Decl(computedPropertyNamesUnionTypes.ts, 53, 9))
>Mapped : Symbol(Mapped, Decl(computedPropertyNamesUnionTypes.ts, 47, 1))
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 52, 9))
}
// Non-literal key should still produce index signature (unchanged behavior)
function dynamicKey(key: string, value: number) {
>dynamicKey : Symbol(dynamicKey, Decl(computedPropertyNamesUnionTypes.ts, 54, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 57, 20))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 57, 32))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 58, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 58, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 57, 20))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 57, 32))
obj.anything; // ok via index signature
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 58, 9))
}
// Template literal key should still produce index signature
function templateKey(key: `prefix_${string}`, value: number) {
>templateKey : Symbol(templateKey, Decl(computedPropertyNamesUnionTypes.ts, 60, 1))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 63, 21))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 63, 45))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 64, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 64, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 63, 21))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 63, 45))
}
// Generic extends literal union should work
function genericKey<K extends 'a' | 'b'>(key: K, value: number) {
>genericKey : Symbol(genericKey, Decl(computedPropertyNamesUnionTypes.ts, 65, 1))
>K : Symbol(K, Decl(computedPropertyNamesUnionTypes.ts, 68, 20))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 68, 41))
>K : Symbol(K, Decl(computedPropertyNamesUnionTypes.ts, 68, 20))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 68, 48))
const obj = { [key]: value };
>obj : Symbol(obj, Decl(computedPropertyNamesUnionTypes.ts, 69, 9))
>[key] : Symbol([key], Decl(computedPropertyNamesUnionTypes.ts, 69, 17))
>key : Symbol(key, Decl(computedPropertyNamesUnionTypes.ts, 68, 41))
>value : Symbol(value, Decl(computedPropertyNamesUnionTypes.ts, 68, 48))
}