|
4 | 4 | type TV0<T extends unknown[]> = [string, ...T]; |
5 | 5 | type TV1<T extends unknown[]> = [string, ...T, number]; |
6 | 6 | type TV2<T extends unknown[]> = [string, ...T, number, ...T]; |
7 | | -type TV3<T extends unknown[]> = [string, ...T, ...number[], ...T]; // Error |
| 7 | +type TV3<T extends unknown[]> = [string, ...T, ...number[], ...T]; |
8 | 8 |
|
9 | 9 | // Normalization |
10 | 10 |
|
@@ -206,11 +206,18 @@ function f15<T extends string[], U extends T>(k0: keyof T, k1: keyof [...T], k2: |
206 | 206 |
|
207 | 207 | // Inference between variadic tuple types |
208 | 208 |
|
209 | | -type First<T extends readonly unknown[]> = T[0]; |
210 | | -type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T]; |
| 209 | +type First<T extends readonly unknown[]> = |
| 210 | + T extends readonly [unknown, ...unknown[]] ? T[0] : |
| 211 | + T[0] | undefined; |
211 | 212 |
|
212 | | -type Last<T extends readonly unknown[]> = T extends readonly [...infer _, infer U] ? U : T extends readonly [...infer _, (infer U)?] ? U | undefined : undefined; |
213 | | -type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?] ? U : [...T]; |
| 213 | +type DropFirst<T extends readonly unknown[]> = T extends readonly [unknown?, ...infer U] ? U : [...T]; |
| 214 | + |
| 215 | +type Last<T extends readonly unknown[]> = |
| 216 | + T extends readonly [...unknown[], infer U] ? U : |
| 217 | + T extends readonly [unknown, ...unknown[]] ? T[number] : |
| 218 | + T[number] | undefined; |
| 219 | + |
| 220 | +type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, unknown] ? U : [...T]; |
214 | 221 |
|
215 | 222 | type T00 = First<[number, symbol, string]>; |
216 | 223 | type T01 = First<[symbol, string]>; |
@@ -241,8 +248,8 @@ type T23 = Last<[number, symbol, ...string[]]>; |
241 | 248 | type T24 = Last<[symbol, ...string[]]>; |
242 | 249 | type T25 = Last<[string?]>; |
243 | 250 | type T26 = Last<string[]>; |
244 | | -type T27 = Last<[]>; // unknown, maybe should undefined |
245 | | -type T28 = Last<any>; // unknown, maybe should be any |
| 251 | +type T27 = Last<[]>; |
| 252 | +type T28 = Last<any>; |
246 | 253 | type T29 = Last<never>; |
247 | 254 |
|
248 | 255 | type T30 = DropLast<[number, symbol, string]>; |
@@ -340,10 +347,6 @@ ft(['a', 'b'], ['c', 'd', 42]) |
340 | 347 | declare function call<T extends unknown[], R>(...args: [...T, (...args: T) => R]): [T, R]; |
341 | 348 |
|
342 | 349 | call('hello', 32, (a, b) => 42); |
343 | | - |
344 | | -// Would be nice to infer [...string[], (...args: string[]) => number] here |
345 | | -// Requires [starting-fixed-part, ...rest-part, ending-fixed-part] tuple structure |
346 | | - |
347 | 350 | call(...sa, (...x) => 42); |
348 | 351 |
|
349 | 352 | // No inference to ending optional elements (except with identical structure) |
@@ -392,7 +395,7 @@ callApi(getOrgUser); |
392 | 395 |
|
393 | 396 | type Numbers = number[]; |
394 | 397 | type Unbounded = [...Numbers, boolean]; |
395 | | -const data: Unbounded = [false, false]; |
| 398 | +const data: Unbounded = [false, false]; // Error |
396 | 399 |
|
397 | 400 | type U1 = [string, ...Numbers, boolean]; |
398 | 401 | type U2 = [...[string, ...Numbers], boolean]; |
@@ -588,8 +591,6 @@ ft([1, 2], [1, 2, 3]); |
588 | 591 | ft(['a', 'b'], ['c', 'd']); |
589 | 592 | ft(['a', 'b'], ['c', 'd', 42]); |
590 | 593 | call('hello', 32, function (a, b) { return 42; }); |
591 | | -// Would be nice to infer [...string[], (...args: string[]) => number] here |
592 | | -// Requires [starting-fixed-part, ...rest-part, ending-fixed-part] tuple structure |
593 | 594 | call.apply(void 0, __spreadArrays(sa, [function () { |
594 | 595 | var x = []; |
595 | 596 | for (var _i = 0; _i < arguments.length; _i++) { |
@@ -619,7 +620,7 @@ function callApi(method) { |
619 | 620 | } |
620 | 621 | callApi(getUser); |
621 | 622 | callApi(getOrgUser); |
622 | | -var data = [false, false]; |
| 623 | +var data = [false, false]; // Error |
623 | 624 |
|
624 | 625 |
|
625 | 626 | //// [variadicTuples1.d.ts] |
@@ -674,10 +675,10 @@ declare function f12<T extends readonly unknown[]>(t: T, m: [...T], r: readonly |
674 | 675 | declare function f13<T extends string[], U extends T>(t0: T, t1: [...T], t2: [...U]): void; |
675 | 676 | declare function f14<T extends readonly string[], U extends T>(t0: T, t1: [...T], t2: [...U]): void; |
676 | 677 | declare function f15<T extends string[], U extends T>(k0: keyof T, k1: keyof [...T], k2: keyof [...U], k3: keyof [1, 2, ...T]): void; |
677 | | -declare type First<T extends readonly unknown[]> = T[0]; |
678 | | -declare type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T]; |
679 | | -declare type Last<T extends readonly unknown[]> = T extends readonly [...infer _, infer U] ? U : T extends readonly [...infer _, (infer U)?] ? U | undefined : undefined; |
680 | | -declare type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?] ? U : [...T]; |
| 678 | +declare type First<T extends readonly unknown[]> = T extends readonly [unknown, ...unknown[]] ? T[0] : T[0] | undefined; |
| 679 | +declare type DropFirst<T extends readonly unknown[]> = T extends readonly [unknown?, ...infer U] ? U : [...T]; |
| 680 | +declare type Last<T extends readonly unknown[]> = T extends readonly [...unknown[], infer U] ? U : T extends readonly [unknown, ...unknown[]] ? T[number] : T[number] | undefined; |
| 681 | +declare type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, unknown] ? U : [...T]; |
681 | 682 | declare type T00 = First<[number, symbol, string]>; |
682 | 683 | declare type T01 = First<[symbol, string]>; |
683 | 684 | declare type T02 = First<[string]>; |
|
0 commit comments