Skip to content

Commit 23de9f2

Browse files
committed
Fix tests
1 parent af4d6f5 commit 23de9f2

File tree

5 files changed

+456
-438
lines changed

5 files changed

+456
-438
lines changed

tests/baselines/reference/variadicTuples1.errors.txt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(191,5): error TS2322: Typ
4040
'U' could be instantiated with an arbitrary type which could be unrelated to 'readonly string[]'.
4141
tests/cases/conformance/types/tuple/variadicTuples1.ts(203,5): error TS2322: Type 'string' is not assignable to type 'keyof [1, 2, ...T]'.
4242
Type '"2"' is not assignable to type 'number | "0" | "length" | "toString" | "toLocaleString" | "pop" | "push" | "concat" | "join" | "reverse" | "shift" | "slice" | "sort" | "splice" | "unshift" | "indexOf" | "lastIndexOf" | "every" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight" | "1"'.
43-
tests/cases/conformance/types/tuple/variadicTuples1.ts(354,26): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
44-
tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Type '[false, false]' is not assignable to type '[...number[], boolean]'.
43+
tests/cases/conformance/types/tuple/variadicTuples1.ts(357,26): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
44+
tests/cases/conformance/types/tuple/variadicTuples1.ts(397,7): error TS2322: Type '[false, false]' is not assignable to type '[...number[], boolean]'.
4545
Type at position 0 in source is not compatible with type at position 0 in target.
4646
Type 'boolean' is not assignable to type 'number'.
4747

@@ -52,7 +52,7 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Typ
5252
type TV0<T extends unknown[]> = [string, ...T];
5353
type TV1<T extends unknown[]> = [string, ...T, number];
5454
type TV2<T extends unknown[]> = [string, ...T, number, ...T];
55-
type TV3<T extends unknown[]> = [string, ...T, ...number[], ...T]; // Error
55+
type TV3<T extends unknown[]> = [string, ...T, ...number[], ...T];
5656

5757
// Normalization
5858

@@ -315,11 +315,18 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Typ
315315

316316
// Inference between variadic tuple types
317317

318-
type First<T extends readonly unknown[]> = T[0];
319-
type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T];
318+
type First<T extends readonly unknown[]> =
319+
T extends readonly [unknown, ...unknown[]] ? T[0] :
320+
T[0] | undefined;
320321

321-
type Last<T extends readonly unknown[]> = T extends readonly [...infer _, infer U] ? U : T extends readonly [...infer _, (infer U)?] ? U | undefined : undefined;
322-
type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?] ? U : [...T];
322+
type DropFirst<T extends readonly unknown[]> = T extends readonly [unknown?, ...infer U] ? U : [...T];
323+
324+
type Last<T extends readonly unknown[]> =
325+
T extends readonly [...unknown[], infer U] ? U :
326+
T extends readonly [unknown, ...unknown[]] ? T[number] :
327+
T[number] | undefined;
328+
329+
type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, unknown] ? U : [...T];
323330

324331
type T00 = First<[number, symbol, string]>;
325332
type T01 = First<[symbol, string]>;
@@ -350,8 +357,8 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Typ
350357
type T24 = Last<[symbol, ...string[]]>;
351358
type T25 = Last<[string?]>;
352359
type T26 = Last<string[]>;
353-
type T27 = Last<[]>; // unknown, maybe should undefined
354-
type T28 = Last<any>; // unknown, maybe should be any
360+
type T27 = Last<[]>;
361+
type T28 = Last<any>;
355362
type T29 = Last<never>;
356363

357364
type T30 = DropLast<[number, symbol, string]>;
@@ -449,10 +456,6 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Typ
449456
declare function call<T extends unknown[], R>(...args: [...T, (...args: T) => R]): [T, R];
450457

451458
call('hello', 32, (a, b) => 42);
452-
453-
// Would be nice to infer [...string[], (...args: string[]) => number] here
454-
// Requires [starting-fixed-part, ...rest-part, ending-fixed-part] tuple structure
455-
456459
call(...sa, (...x) => 42);
457460

458461
// No inference to ending optional elements (except with identical structure)
@@ -503,7 +506,7 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Typ
503506

504507
type Numbers = number[];
505508
type Unbounded = [...Numbers, boolean];
506-
const data: Unbounded = [false, false];
509+
const data: Unbounded = [false, false]; // Error
507510
~~~~
508511
!!! error TS2322: Type '[false, false]' is not assignable to type '[...number[], boolean]'.
509512
!!! error TS2322: Type at position 0 in source is not compatible with type at position 0 in target.

tests/baselines/reference/variadicTuples1.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
type TV0<T extends unknown[]> = [string, ...T];
55
type TV1<T extends unknown[]> = [string, ...T, number];
66
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];
88

99
// Normalization
1010

@@ -206,11 +206,18 @@ function f15<T extends string[], U extends T>(k0: keyof T, k1: keyof [...T], k2:
206206

207207
// Inference between variadic tuple types
208208

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;
211212

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];
214221

215222
type T00 = First<[number, symbol, string]>;
216223
type T01 = First<[symbol, string]>;
@@ -241,8 +248,8 @@ type T23 = Last<[number, symbol, ...string[]]>;
241248
type T24 = Last<[symbol, ...string[]]>;
242249
type T25 = Last<[string?]>;
243250
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>;
246253
type T29 = Last<never>;
247254

248255
type T30 = DropLast<[number, symbol, string]>;
@@ -340,10 +347,6 @@ ft(['a', 'b'], ['c', 'd', 42])
340347
declare function call<T extends unknown[], R>(...args: [...T, (...args: T) => R]): [T, R];
341348

342349
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-
347350
call(...sa, (...x) => 42);
348351

349352
// No inference to ending optional elements (except with identical structure)
@@ -392,7 +395,7 @@ callApi(getOrgUser);
392395

393396
type Numbers = number[];
394397
type Unbounded = [...Numbers, boolean];
395-
const data: Unbounded = [false, false];
398+
const data: Unbounded = [false, false]; // Error
396399

397400
type U1 = [string, ...Numbers, boolean];
398401
type U2 = [...[string, ...Numbers], boolean];
@@ -588,8 +591,6 @@ ft([1, 2], [1, 2, 3]);
588591
ft(['a', 'b'], ['c', 'd']);
589592
ft(['a', 'b'], ['c', 'd', 42]);
590593
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
593594
call.apply(void 0, __spreadArrays(sa, [function () {
594595
var x = [];
595596
for (var _i = 0; _i < arguments.length; _i++) {
@@ -619,7 +620,7 @@ function callApi(method) {
619620
}
620621
callApi(getUser);
621622
callApi(getOrgUser);
622-
var data = [false, false];
623+
var data = [false, false]; // Error
623624

624625

625626
//// [variadicTuples1.d.ts]
@@ -674,10 +675,10 @@ declare function f12<T extends readonly unknown[]>(t: T, m: [...T], r: readonly
674675
declare function f13<T extends string[], U extends T>(t0: T, t1: [...T], t2: [...U]): void;
675676
declare function f14<T extends readonly string[], U extends T>(t0: T, t1: [...T], t2: [...U]): void;
676677
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];
681682
declare type T00 = First<[number, symbol, string]>;
682683
declare type T01 = First<[symbol, string]>;
683684
declare type T02 = First<[string]>;

0 commit comments

Comments
 (0)