File: recursiveConditionalTypes.types

package info (click to toggle)
node-typescript 4.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 396,552 kB
  • sloc: javascript: 1,444,377; makefile: 7; sh: 3
file content (312 lines) | stat: -rw-r--r-- 8,879 bytes parent folder | download
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
=== tests/cases/compiler/recursiveConditionalTypes.ts ===
// Awaiting promises

type Awaited<T> =
>Awaited : Awaited<T>

    T extends null | undefined ? T :
>null : null

    T extends PromiseLike<infer U> ? Awaited<U> :
    T;

type MyPromise<T> = {
>MyPromise : MyPromise<T>

    then<U>(f: ((value: T) => U | PromiseLike<U>) | null | undefined): MyPromise<U>;
>then : <U>(f: ((value: T) => U | PromiseLike<U>) | null | undefined) => MyPromise<U>
>f : ((value: T) => U | PromiseLike<U>) | null | undefined
>value : T
>null : null
}

type InfinitePromise<T> = Promise<InfinitePromise<T>>;
>InfinitePromise : InfinitePromise<T>

type P0 = Awaited<Promise<string | Promise<MyPromise<number> | null> | undefined>>;
>P0 : string | number | null | undefined
>null : null

type P1 = Awaited<any>;
>P1 : any

type P2 = Awaited<InfinitePromise<number>>;  // Error
>P2 : any

function f11<T, U extends T>(tx: T, ta: Awaited<T>, ux: U, ua: Awaited<U>) {
>f11 : <T, U extends T>(tx: T, ta: Awaited<T>, ux: U, ua: Awaited<U>) => void
>tx : T
>ta : Awaited<T>
>ux : U
>ua : Awaited<U>

    ta = ua;
>ta = ua : Awaited<U>
>ta : Awaited<T>
>ua : Awaited<U>

    ua = ta;  // Error
>ua = ta : Awaited<T>
>ua : Awaited<U>
>ta : Awaited<T>

    ta = tx;  // Error
>ta = tx : T
>ta : Awaited<T>
>tx : T

    tx = ta;  // Error
>tx = ta : Awaited<T>
>tx : T
>ta : Awaited<T>
}

// Flattening arrays

type Flatten<T extends readonly unknown[]> = T extends unknown[] ? _Flatten<T>[] : readonly _Flatten<T>[];
>Flatten : Flatten<T>

type _Flatten<T> = T extends readonly (infer U)[] ? _Flatten<U> : T;
>_Flatten : _Flatten<T>

type InfiniteArray<T> = InfiniteArray<T>[];
>InfiniteArray : InfiniteArray<T>

type B0 = Flatten<string[][][]>;
>B0 : string[]

type B1 = Flatten<string[][] | readonly (number[] | boolean[][])[]>;
>B1 : string[] | readonly (number | boolean)[]

type B2 = Flatten<InfiniteArray<string>>;
>B2 : any[]

type B3 = B2[0];  // Error
>B3 : any

// Repeating tuples

type TupleOf<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
>TupleOf : TupleOf<T, N>

type _TupleOf<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : _TupleOf<T, N, [T, ...R]>;
>_TupleOf : _TupleOf<T, N, R>

type TT0 = TupleOf<string, 4>;
>TT0 : [string, string, string, string]

type TT1 = TupleOf<number, 0 | 2 | 4>;
>TT1 : [] | [number, number] | [number, number, number, number]

type TT2 = TupleOf<number, number>;
>TT2 : number[]

type TT3 = TupleOf<number, any>;
>TT3 : number[]

type TT4 = TupleOf<number, 100>;  // Depth error
>TT4 : any

function f22<N extends number, M extends N>(tn: TupleOf<number, N>, tm: TupleOf<number, M>) {
>f22 : <N extends number, M extends N>(tn: TupleOf<number, N>, tm: TupleOf<number, M>) => void
>tn : TupleOf<number, N>
>tm : TupleOf<number, M>

    tn = tm;
>tn = tm : TupleOf<number, M>
>tn : TupleOf<number, N>
>tm : TupleOf<number, M>

    tm = tn;
>tm = tn : TupleOf<number, N>
>tm : TupleOf<number, M>
>tn : TupleOf<number, N>
}

declare function f23<T>(t: TupleOf<T, 3>): T;
>f23 : <T>(t: [T, T, T]) => T
>t : [T, T, T]

f23(['a', 'b', 'c']);  // string
>f23(['a', 'b', 'c']) : string
>f23 : <T>(t: [T, T, T]) => T
>['a', 'b', 'c'] : [string, string, string]
>'a' : "a"
>'b' : "b"
>'c' : "c"

// Inference to recursive type

interface Box<T> { value: T };
>value : T

type RecBox<T> = T | Box<RecBox<T>>;
>RecBox : RecBox<T>

type InfBox<T> = Box<InfBox<T>>;
>InfBox : InfBox<T>

declare function unbox<T>(box: RecBox<T>): T
>unbox : <T>(box: RecBox<T>) => T
>box : RecBox<T>

type T1 = Box<string>;
>T1 : T1

type T2 = Box<T1>;
>T2 : T2

type T3 = Box<T2>;
>T3 : T3

type T4 = Box<T3>;
>T4 : T4

type T5 = Box<T4>;
>T5 : T5

type T6 = Box<T5>;
>T6 : T6

declare let b1: Box<Box<Box<Box<Box<Box<string>>>>>>;
>b1 : Box<Box<Box<Box<Box<Box<string>>>>>>

declare let b2: T6;
>b2 : T6

declare let b3: InfBox<string>;
>b3 : InfBox<string>

declare let b4: { value: { value: { value: typeof b4 }}};
>b4 : { value: {    value: {        value: typeof b4;    };}; }
>value : { value: {    value: typeof b4;}; }
>value : { value: typeof b4; }
>value : { value: { value: { value: typeof b4; }; }; }
>b4 : { value: { value: { value: any; }; }; }

unbox(b1);  // string
>unbox(b1) : string
>unbox : <T>(box: RecBox<T>) => T
>b1 : Box<Box<Box<Box<Box<Box<string>>>>>>

unbox(b2);  // string
>unbox(b2) : string
>unbox : <T>(box: RecBox<T>) => T
>b2 : T6

unbox(b3);  // InfBox<string>
>unbox(b3) : InfBox<string>
>unbox : <T>(box: RecBox<T>) => T
>b3 : InfBox<string>

unbox({ value: { value: { value: { value: { value: { value: 5 }}}}}});  // number
>unbox({ value: { value: { value: { value: { value: { value: 5 }}}}}}) : number
>unbox : <T>(box: RecBox<T>) => T
>{ value: { value: { value: { value: { value: { value: 5 }}}}}} : { value: { value: { value: { value: { value: { value: number; }; }; }; }; }; }
>value : { value: { value: { value: { value: { value: number; }; }; }; }; }
>{ value: { value: { value: { value: { value: 5 }}}}} : { value: { value: { value: { value: { value: number; }; }; }; }; }
>value : { value: { value: { value: { value: number; }; }; }; }
>{ value: { value: { value: { value: 5 }}}} : { value: { value: { value: { value: number; }; }; }; }
>value : { value: { value: { value: number; }; }; }
>{ value: { value: { value: 5 }}} : { value: { value: { value: number; }; }; }
>value : { value: { value: number; }; }
>{ value: { value: 5 }} : { value: { value: number; }; }
>value : { value: number; }
>{ value: 5 } : { value: number; }
>value : number
>5 : 5

unbox(b4);  // { value: { value: typeof b4 }}
>unbox(b4) : { value: { value: { value: any; }; }; }
>unbox : <T>(box: RecBox<T>) => T
>b4 : { value: { value: { value: any; }; }; }

unbox({ value: { value: { get value() { return this; } }}});  // { readonly value: ... }
>unbox({ value: { value: { get value() { return this; } }}}) : { readonly value: { readonly value: any; }; }
>unbox : <T>(box: RecBox<T>) => T
>{ value: { value: { get value() { return this; } }}} : { value: { value: { readonly value: { readonly value: any; }; }; }; }
>value : { value: { readonly value: { readonly value: any; }; }; }
>{ value: { get value() { return this; } }} : { value: { readonly value: { readonly value: any; }; }; }
>value : { readonly value: { readonly value: any; }; }
>{ get value() { return this; } } : { readonly value: { readonly value: any; }; }
>value : { readonly value: any; }
>this : { readonly value: any; } | { readonly value: { readonly value: any; }; } | Box<RecBox<{ readonly value: { readonly value: any; }; }>>

// Inference from nested instantiations of same generic types

type Box1<T> = { value: T };
>Box1 : Box1<T>
>value : T

type Box2<T> = { value: T };
>Box2 : Box2<T>
>value : T

declare function foo<T>(x: Box1<Box1<T>>): T;
>foo : <T>(x: Box1<Box1<T>>) => T
>x : Box1<Box1<T>>

declare let z: Box2<Box2<string>>;
>z : Box2<Box2<string>>

foo(z);  // unknown, but ideally would be string (requires unique recursion ID for each type reference)
>foo(z) : unknown
>foo : <T>(x: Box1<Box1<T>>) => T
>z : Box2<Box2<string>>

// Intersect tuple element types

type Intersect<U extends any[], R = unknown> = U extends [infer H, ...infer T] ? Intersect<T, R & H> : R;
>Intersect : Intersect<U, R>

type QQ = Intersect<[string[], number[], 7]>;
>QQ : string[] & number[] & 7

// Infer between structurally identical recursive conditional types

type Unpack1<T> = T extends (infer U)[] ? Unpack1<U> : T;
>Unpack1 : Unpack1<T>

type Unpack2<T> = T extends (infer U)[] ? Unpack2<U> : T;
>Unpack2 : Unpack2<T>

function f20<T, U extends T>(x: Unpack1<T>, y: Unpack2<T>) {
>f20 : <T, U extends T>(x: Unpack1<T>, y: Unpack2<T>) => void
>x : Unpack1<T>
>y : Unpack2<T>

    x = y;
>x = y : Unpack2<T>
>x : Unpack1<T>
>y : Unpack2<T>

    y = x;
>y = x : Unpack1<T>
>y : Unpack2<T>
>x : Unpack1<T>

    f20(y, x);
>f20(y, x) : void
>f20 : <T, U extends T>(x: Unpack1<T>, y: Unpack2<T>) => void
>y : Unpack2<T>
>x : Unpack1<T>
}

type Grow1<T extends unknown[], N extends number> = T['length'] extends N ? T : Grow1<[number, ...T], N>;
>Grow1 : Grow1<T, N>

type Grow2<T extends unknown[], N extends number> = T['length'] extends N ? T : Grow2<[string, ...T], N>;
>Grow2 : Grow2<T, N>

function f21<T extends number>(x: Grow1<[], T>, y: Grow2<[], T>) {
>f21 : <T extends number>(x: Grow1<[], T>, y: Grow2<[], T>) => void
>x : Grow1<[], T>
>y : Grow2<[], T>

    f21(y, x);  // Error
>f21(y, x) : void
>f21 : <T extends number>(x: Grow1<[], T>, y: Grow2<[], T>) => void
>y : Grow2<[], T>
>x : Grow1<[], T>
}