Skip to content

Commit 5b6f969

Browse files
committed
Add tests
1 parent 4bf6ae7 commit 5b6f969

3 files changed

+688
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
//// [tests/cases/compiler/typeVariableConstraintIntersections.ts] ////
2+
3+
=== typeVariableConstraintIntersections.ts ===
4+
type T00<K extends "a" | "b"> = K & "a";
5+
>T00 : Symbol(T00, Decl(typeVariableConstraintIntersections.ts, 0, 0))
6+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 0, 9))
7+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 0, 9))
8+
9+
type T01<K extends "a" | "b"> = K & "c";
10+
>T01 : Symbol(T01, Decl(typeVariableConstraintIntersections.ts, 0, 40))
11+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 1, 9))
12+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 1, 9))
13+
14+
type T02<K extends "a" | "b"> = K & string;
15+
>T02 : Symbol(T02, Decl(typeVariableConstraintIntersections.ts, 1, 40))
16+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 2, 9))
17+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 2, 9))
18+
19+
type T10<K extends string> = K & "a";
20+
>T10 : Symbol(T10, Decl(typeVariableConstraintIntersections.ts, 2, 43))
21+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 4, 9))
22+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 4, 9))
23+
24+
type T11<K extends string> = K & "c";
25+
>T11 : Symbol(T11, Decl(typeVariableConstraintIntersections.ts, 4, 37))
26+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 5, 9))
27+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 5, 9))
28+
29+
type T12<K extends string> = K & string;
30+
>T12 : Symbol(T12, Decl(typeVariableConstraintIntersections.ts, 5, 37))
31+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 6, 9))
32+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 6, 9))
33+
34+
type T20<K extends "a" | "b" | "c"> = K & ("a" | "b" | "c");
35+
>T20 : Symbol(T20, Decl(typeVariableConstraintIntersections.ts, 6, 40))
36+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 8, 9))
37+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 8, 9))
38+
39+
type T21<K extends "a" | "b" | "c"> = ("a" | "b" | "c") & K;
40+
>T21 : Symbol(T21, Decl(typeVariableConstraintIntersections.ts, 8, 60))
41+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 9, 9))
42+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 9, 9))
43+
44+
type T22<K extends "a" | "b" | "c"> = K & ("a" | "b");
45+
>T22 : Symbol(T22, Decl(typeVariableConstraintIntersections.ts, 9, 60))
46+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 10, 9))
47+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 10, 9))
48+
49+
type T23<K extends "a" | "b" | "c"> = ("a" | "b") & K;
50+
>T23 : Symbol(T23, Decl(typeVariableConstraintIntersections.ts, 10, 54))
51+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 11, 9))
52+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 11, 9))
53+
54+
type T30<K extends "a" | "b"> = K & ("a" | "b" | "c");
55+
>T30 : Symbol(T30, Decl(typeVariableConstraintIntersections.ts, 11, 54))
56+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 13, 9))
57+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 13, 9))
58+
59+
type T31<K extends "a" | "b"> = ("a" | "b" | "c") & K;
60+
>T31 : Symbol(T31, Decl(typeVariableConstraintIntersections.ts, 13, 54))
61+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 14, 9))
62+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 14, 9))
63+
64+
type T32<K extends "a" | "b"> = K & ("a" | "b");
65+
>T32 : Symbol(T32, Decl(typeVariableConstraintIntersections.ts, 14, 54))
66+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 15, 9))
67+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 15, 9))
68+
69+
type T33<K extends "a" | "b"> = ("a" | "b") & K;
70+
>T33 : Symbol(T33, Decl(typeVariableConstraintIntersections.ts, 15, 48))
71+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 16, 9))
72+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 16, 9))
73+
74+
type T40<K extends {}> = K & undefined;
75+
>T40 : Symbol(T40, Decl(typeVariableConstraintIntersections.ts, 16, 48))
76+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 18, 9))
77+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 18, 9))
78+
79+
type T41<K extends {}> = K & null;
80+
>T41 : Symbol(T41, Decl(typeVariableConstraintIntersections.ts, 18, 39))
81+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 19, 9))
82+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 19, 9))
83+
84+
type T42<K extends {}> = K & object;
85+
>T42 : Symbol(T42, Decl(typeVariableConstraintIntersections.ts, 19, 34))
86+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 20, 9))
87+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 20, 9))
88+
89+
type T43<K extends {}> = K & {};
90+
>T43 : Symbol(T43, Decl(typeVariableConstraintIntersections.ts, 20, 36))
91+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 21, 9))
92+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 21, 9))
93+
94+
type T50<K extends "a" | 0> = K & "a";
95+
>T50 : Symbol(T50, Decl(typeVariableConstraintIntersections.ts, 21, 32))
96+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 23, 9))
97+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 23, 9))
98+
99+
type T51<K extends "a" | 0> = K & "b";
100+
>T51 : Symbol(T51, Decl(typeVariableConstraintIntersections.ts, 23, 38))
101+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 24, 9))
102+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 24, 9))
103+
104+
type T52<K extends "a" | 0> = K & string;
105+
>T52 : Symbol(T52, Decl(typeVariableConstraintIntersections.ts, 24, 38))
106+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 25, 9))
107+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 25, 9))
108+
109+
type T53<K extends "a" | 0> = K & 0;
110+
>T53 : Symbol(T53, Decl(typeVariableConstraintIntersections.ts, 25, 41))
111+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 26, 9))
112+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 26, 9))
113+
114+
type T54<K extends "a" | 0> = K & 1;
115+
>T54 : Symbol(T54, Decl(typeVariableConstraintIntersections.ts, 26, 36))
116+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 27, 9))
117+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 27, 9))
118+
119+
type T55<K extends "a" | 0> = K & number;
120+
>T55 : Symbol(T55, Decl(typeVariableConstraintIntersections.ts, 27, 36))
121+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 28, 9))
122+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 28, 9))
123+
124+
type T60<T extends "a" | "b", U extends T> = U & "a";
125+
>T60 : Symbol(T60, Decl(typeVariableConstraintIntersections.ts, 28, 41))
126+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 30, 9))
127+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 30, 29))
128+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 30, 9))
129+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 30, 29))
130+
131+
type T61<T extends "a" | "b", U extends T> = U & ("a" | "b");
132+
>T61 : Symbol(T61, Decl(typeVariableConstraintIntersections.ts, 30, 53))
133+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 31, 9))
134+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 31, 29))
135+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 31, 9))
136+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 31, 29))
137+
138+
type T62<T extends "a" | "b", U extends T> = U & ("a" | "b" | "c");
139+
>T62 : Symbol(T62, Decl(typeVariableConstraintIntersections.ts, 31, 61))
140+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 32, 9))
141+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 32, 29))
142+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 32, 9))
143+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 32, 29))
144+
145+
type T63<T extends "a" | "b", U extends T> = U & string;
146+
>T63 : Symbol(T63, Decl(typeVariableConstraintIntersections.ts, 32, 67))
147+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 33, 9))
148+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 33, 29))
149+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 33, 9))
150+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 33, 29))
151+
152+
type T70<T extends "a" | "b", U extends T | "c"> = U & "a";
153+
>T70 : Symbol(T70, Decl(typeVariableConstraintIntersections.ts, 33, 56))
154+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 35, 9))
155+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 35, 29))
156+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 35, 9))
157+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 35, 29))
158+
159+
type T71<T extends "a" | "b", U extends T | "c"> = U & ("a" | "b");
160+
>T71 : Symbol(T71, Decl(typeVariableConstraintIntersections.ts, 35, 59))
161+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 36, 9))
162+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 36, 29))
163+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 36, 9))
164+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 36, 29))
165+
166+
type T72<T extends "a" | "b", U extends T | "c"> = U & ("a" | "b" | "c");
167+
>T72 : Symbol(T72, Decl(typeVariableConstraintIntersections.ts, 36, 67))
168+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 37, 9))
169+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 37, 29))
170+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 37, 9))
171+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 37, 29))
172+
173+
type T73<T extends "a" | "b", U extends T | "c"> = U & string;
174+
>T73 : Symbol(T73, Decl(typeVariableConstraintIntersections.ts, 37, 73))
175+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 38, 9))
176+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 38, 29))
177+
>T : Symbol(T, Decl(typeVariableConstraintIntersections.ts, 38, 9))
178+
>U : Symbol(U, Decl(typeVariableConstraintIntersections.ts, 38, 29))
179+
180+
declare function isA(x: any): x is "a";
181+
>isA : Symbol(isA, Decl(typeVariableConstraintIntersections.ts, 38, 62))
182+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 40, 21))
183+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 40, 21))
184+
185+
declare function isB(x: any): x is "b";
186+
>isB : Symbol(isB, Decl(typeVariableConstraintIntersections.ts, 40, 39))
187+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 41, 21))
188+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 41, 21))
189+
190+
declare function isC(x: any): x is "c";
191+
>isC : Symbol(isC, Decl(typeVariableConstraintIntersections.ts, 41, 39))
192+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 42, 21))
193+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 42, 21))
194+
195+
function foo<K extends "a" | "b">(x: K) {
196+
>foo : Symbol(foo, Decl(typeVariableConstraintIntersections.ts, 42, 39))
197+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 44, 13))
198+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
199+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 44, 13))
200+
201+
if (isA(x)) {
202+
>isA : Symbol(isA, Decl(typeVariableConstraintIntersections.ts, 38, 62))
203+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
204+
205+
x; // K & "a"
206+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
207+
}
208+
if (isB(x)) {
209+
>isB : Symbol(isB, Decl(typeVariableConstraintIntersections.ts, 40, 39))
210+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
211+
212+
x; // K & "b"
213+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
214+
}
215+
if (isC(x)) {
216+
>isC : Symbol(isC, Decl(typeVariableConstraintIntersections.ts, 41, 39))
217+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
218+
219+
x; // never
220+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
221+
}
222+
if (isA(x) || isB(x)) {
223+
>isA : Symbol(isA, Decl(typeVariableConstraintIntersections.ts, 38, 62))
224+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
225+
>isB : Symbol(isB, Decl(typeVariableConstraintIntersections.ts, 40, 39))
226+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
227+
228+
x; // K
229+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
230+
}
231+
if (!(isA(x) || isB(x))) {
232+
>isA : Symbol(isA, Decl(typeVariableConstraintIntersections.ts, 38, 62))
233+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
234+
>isB : Symbol(isB, Decl(typeVariableConstraintIntersections.ts, 40, 39))
235+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
236+
237+
return;
238+
}
239+
x; // K
240+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 44, 34))
241+
}
242+
243+
// Example from #30581
244+
245+
type OptionOne = {
246+
>OptionOne : Symbol(OptionOne, Decl(typeVariableConstraintIntersections.ts, 61, 1))
247+
248+
kind: "one";
249+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 65, 18))
250+
251+
s: string;
252+
>s : Symbol(s, Decl(typeVariableConstraintIntersections.ts, 66, 14))
253+
254+
};
255+
256+
type OptionTwo = {
257+
>OptionTwo : Symbol(OptionTwo, Decl(typeVariableConstraintIntersections.ts, 68, 2))
258+
259+
kind: "two";
260+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 70, 18))
261+
262+
x: number;
263+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 71, 14))
264+
265+
y: number;
266+
>y : Symbol(y, Decl(typeVariableConstraintIntersections.ts, 72, 12))
267+
268+
};
269+
270+
type Options = OptionOne | OptionTwo;
271+
>Options : Symbol(Options, Decl(typeVariableConstraintIntersections.ts, 74, 2))
272+
>OptionOne : Symbol(OptionOne, Decl(typeVariableConstraintIntersections.ts, 61, 1))
273+
>OptionTwo : Symbol(OptionTwo, Decl(typeVariableConstraintIntersections.ts, 68, 2))
274+
275+
type OptionHandlers = {
276+
>OptionHandlers : Symbol(OptionHandlers, Decl(typeVariableConstraintIntersections.ts, 76, 37))
277+
278+
[K in Options['kind']]: (option: Options & { kind: K }) => string;
279+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 79, 3))
280+
>Options : Symbol(Options, Decl(typeVariableConstraintIntersections.ts, 74, 2))
281+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 79, 27))
282+
>Options : Symbol(Options, Decl(typeVariableConstraintIntersections.ts, 74, 2))
283+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 79, 46))
284+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 79, 3))
285+
}
286+
287+
const optionHandlers: OptionHandlers = {
288+
>optionHandlers : Symbol(optionHandlers, Decl(typeVariableConstraintIntersections.ts, 82, 5))
289+
>OptionHandlers : Symbol(OptionHandlers, Decl(typeVariableConstraintIntersections.ts, 76, 37))
290+
291+
"one": option => option.s,
292+
>"one" : Symbol("one", Decl(typeVariableConstraintIntersections.ts, 82, 40))
293+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 83, 8))
294+
>option.s : Symbol(s, Decl(typeVariableConstraintIntersections.ts, 66, 14))
295+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 83, 8))
296+
>s : Symbol(s, Decl(typeVariableConstraintIntersections.ts, 66, 14))
297+
298+
"two": option => option.x + "," + option.y,
299+
>"two" : Symbol("two", Decl(typeVariableConstraintIntersections.ts, 83, 28))
300+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 84, 8))
301+
>option.x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 71, 14))
302+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 84, 8))
303+
>x : Symbol(x, Decl(typeVariableConstraintIntersections.ts, 71, 14))
304+
>option.y : Symbol(y, Decl(typeVariableConstraintIntersections.ts, 72, 12))
305+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 84, 8))
306+
>y : Symbol(y, Decl(typeVariableConstraintIntersections.ts, 72, 12))
307+
308+
};
309+
310+
function handleOption<K extends Options['kind']>(option: Options & { kind: K }): string {
311+
>handleOption : Symbol(handleOption, Decl(typeVariableConstraintIntersections.ts, 85, 2))
312+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 87, 22))
313+
>Options : Symbol(Options, Decl(typeVariableConstraintIntersections.ts, 74, 2))
314+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 87, 49))
315+
>Options : Symbol(Options, Decl(typeVariableConstraintIntersections.ts, 74, 2))
316+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 87, 68))
317+
>K : Symbol(K, Decl(typeVariableConstraintIntersections.ts, 87, 22))
318+
319+
const kind = option.kind;
320+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 88, 7))
321+
>option.kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 65, 18), Decl(typeVariableConstraintIntersections.ts, 87, 68), Decl(typeVariableConstraintIntersections.ts, 70, 18), Decl(typeVariableConstraintIntersections.ts, 87, 68))
322+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 87, 49))
323+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 65, 18), Decl(typeVariableConstraintIntersections.ts, 87, 68), Decl(typeVariableConstraintIntersections.ts, 70, 18), Decl(typeVariableConstraintIntersections.ts, 87, 68))
324+
325+
const handler = optionHandlers[kind];
326+
>handler : Symbol(handler, Decl(typeVariableConstraintIntersections.ts, 89, 7))
327+
>optionHandlers : Symbol(optionHandlers, Decl(typeVariableConstraintIntersections.ts, 82, 5))
328+
>kind : Symbol(kind, Decl(typeVariableConstraintIntersections.ts, 88, 7))
329+
330+
return handler(option);
331+
>handler : Symbol(handler, Decl(typeVariableConstraintIntersections.ts, 89, 7))
332+
>option : Symbol(option, Decl(typeVariableConstraintIntersections.ts, 87, 49))
333+
334+
};
335+

0 commit comments

Comments
 (0)