forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlFlowGenericTypes.ts
141 lines (117 loc) · 2.71 KB
/
controlFlowGenericTypes.ts
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
// @strict: true
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);
}