forked from microsoft/TypeScript
/
controlFlowGenericTypes.js
381 lines (338 loc) · 8.21 KB
/
controlFlowGenericTypes.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//// [controlFlowGenericTypes.ts]
function f1<T extends string | undefined>(x: T, y: { a: T }, z: [T]): string {
if (x) {
x;
x.length;
return x;
}
if (y.a) {
y.a.length;
return y.a;
}
if (z[0]) {
z[0].length;
return z[0];
}
return "hello";
}
function f2<T>(x: Extract<T, string | undefined> | null): string {
if (x) {
x;
x.length;
return x;
}
return "hello";
}
interface Box<T> {
item: T;
}
declare function isBox(x: any): x is Box<unknown>;
declare function isUndefined(x: unknown): x is undefined;
declare function unbox<T>(x: Box<T>): T;
function g1<T extends Box<T> | undefined>(x: T) {
if (isBox(x)) {
unbox(x);
}
}
function g2<T extends Box<T> | undefined>(x: T) {
if (!isUndefined(x)) {
unbox(x);
}
}
function g3<T extends Box<T> | undefined>(x: T) {
if (!isBox(x)) {
unbox(x); // Error
}
}
function g4<T extends Box<T> | undefined>(x: T) {
if (isUndefined(x)) {
unbox(x); // Error
}
}
// Repro from #13995
declare function takeA(val: 'A'): void;
export function bounceAndTakeIfA<AB extends 'A' | 'B'>(value: AB): AB {
if (value === 'A') {
takeA(value);
return value;
}
else {
return value;
}
}
// Repro from #13995
type Common = { id: number };
type AA = { tag: 'A', id: number };
type BB = { tag: 'B', id: number, foo: number };
type MyUnion = AA | BB;
const fn = (value: MyUnion) => {
value.foo; // Error
if ('foo' in value) {
value.foo;
}
if (value.tag === 'B') {
value.foo;
}
};
const fn2 = <T extends MyUnion>(value: T): MyUnion => {
value.foo; // Error
if ('foo' in value) {
value.foo;
}
if (value.tag === 'B') {
value.foo;
}
};
// Repro from #13995
type A1 = {
testable: true
doTest: () => void
}
type B1 = {
testable: false
};
type Union = A1 | B1
function notWorking<T extends Union>(object: T) {
if (!object.testable) return;
object.doTest();
}
// Repro from #42939
interface A {
a: number | null;
};
function get<K extends keyof A>(key: K, obj: A): number {
const value = obj[key];
if (value !== null) {
return value;
}
return 0;
};
// Repro from #44093
class EventEmitter<ET> {
off<K extends keyof ET>(...args: [K, number] | [unknown, string]):void {}
}
function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyof ET): void {
emittingObject.off(eventName, 0);
emittingObject.off(eventName as typeof eventName, 0);
}
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
// and x are of generic types T and K, we want the resulting type to be T[K].
function fx1<T, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
}
function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
}
function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key]; // Error
const x2 = obj && obj[key];
}
// Repro from #44166
class TableBaseEnum<
PublicSpec extends Record<keyof InternalSpec, any>,
InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
m() {
let iSpec = null! as InternalSpec;
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
if (iSpec === undefined) {
return;
}
iSpec[null! as keyof InternalSpec];
iSpec[null! as keyof PublicSpec];
}
}
// Repros from #45145
function f10<T extends { a: string } | undefined>(x: T, y: Partial<T>) {
y = x;
}
type SqlInsertSet<T> = T extends undefined ? object : { [P in keyof T]: unknown };
class SqlTable<T> {
protected validateRow(_row: Partial<SqlInsertSet<T>>): void {
}
public insertRow(row: SqlInsertSet<T>) {
this.validateRow(row);
}
}
// Repro from #46495
interface Button {
type: "button";
text: string;
}
interface Checkbox {
type: "checkbox";
isChecked: boolean;
}
type Control = Button | Checkbox;
function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void {
if (control !== undefined) {
control[key] = value;
}
}
// Repro from #50465
type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; }
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
return column[key];
}
//// [controlFlowGenericTypes.js]
"use strict";
exports.__esModule = true;
exports.bounceAndTakeIfA = void 0;
function f1(x, y, z) {
if (x) {
x;
x.length;
return x;
}
if (y.a) {
y.a.length;
return y.a;
}
if (z[0]) {
z[0].length;
return z[0];
}
return "hello";
}
function f2(x) {
if (x) {
x;
x.length;
return x;
}
return "hello";
}
function g1(x) {
if (isBox(x)) {
unbox(x);
}
}
function g2(x) {
if (!isUndefined(x)) {
unbox(x);
}
}
function g3(x) {
if (!isBox(x)) {
unbox(x); // Error
}
}
function g4(x) {
if (isUndefined(x)) {
unbox(x); // Error
}
}
function bounceAndTakeIfA(value) {
if (value === 'A') {
takeA(value);
return value;
}
else {
return value;
}
}
exports.bounceAndTakeIfA = bounceAndTakeIfA;
var fn = function (value) {
value.foo; // Error
if ('foo' in value) {
value.foo;
}
if (value.tag === 'B') {
value.foo;
}
};
var fn2 = function (value) {
value.foo; // Error
if ('foo' in value) {
value.foo;
}
if (value.tag === 'B') {
value.foo;
}
};
function notWorking(object) {
if (!object.testable)
return;
object.doTest();
}
;
function get(key, obj) {
var value = obj[key];
if (value !== null) {
return value;
}
return 0;
}
;
// Repro from #44093
var EventEmitter = /** @class */ (function () {
function EventEmitter() {
}
EventEmitter.prototype.off = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
};
return EventEmitter;
}());
function once(emittingObject, eventName) {
emittingObject.off(eventName, 0);
emittingObject.off(eventName, 0);
}
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
// and x are of generic types T and K, we want the resulting type to be T[K].
function fx1(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
}
function fx2(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
}
function fx3(obj, key) {
var x1 = obj[key]; // Error
var x2 = obj && obj[key];
}
// Repro from #44166
var TableBaseEnum = /** @class */ (function () {
function TableBaseEnum() {
}
TableBaseEnum.prototype.m = function () {
var iSpec = null;
iSpec[null]; // Error, object possibly undefined
iSpec[null]; // Error, object possibly undefined
if (iSpec === undefined) {
return;
}
iSpec[null];
iSpec[null];
};
return TableBaseEnum;
}());
// Repros from #45145
function f10(x, y) {
y = x;
}
var SqlTable = /** @class */ (function () {
function SqlTable() {
}
SqlTable.prototype.validateRow = function (_row) {
};
SqlTable.prototype.insertRow = function (row) {
this.validateRow(row);
};
return SqlTable;
}());
function update(control, key, value) {
if (control !== undefined) {
control[key] = value;
}
}
function getColumnProperty(column, key) {
return column[key];
}