Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
ahejlsberg committed Nov 14, 2020
commit 23de9f25ca5fe5e977f42097893befc50d33904a
31 changes: 17 additions & 14 deletions tests/baselines/reference/variadicTuples1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(191,5): error TS2322: Typ
'U' could be instantiated with an arbitrary type which could be unrelated to 'readonly string[]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(203,5): error TS2322: Type 'string' is not assignable to type 'keyof [1, 2, ...T]'.
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"'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(354,26): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Type '[false, false]' is not assignable to type '[...number[], boolean]'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(357,26): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
tests/cases/conformance/types/tuple/variadicTuples1.ts(397,7): error TS2322: Type '[false, false]' is not assignable to type '[...number[], boolean]'.
Type at position 0 in source is not compatible with type at position 0 in target.
Type 'boolean' is not assignable to type 'number'.

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

// Normalization

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

// Inference between variadic tuple types

type First<T extends readonly unknown[]> = T[0];
type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T];
type First<T extends readonly unknown[]> =
T extends readonly [unknown, ...unknown[]] ? T[0] :
T[0] | undefined;

type Last<T extends readonly unknown[]> = T extends readonly [...infer _, infer U] ? U : T extends readonly [...infer _, (infer U)?] ? U | undefined : undefined;
type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?] ? U : [...T];
type DropFirst<T extends readonly unknown[]> = T extends readonly [unknown?, ...infer U] ? U : [...T];

type Last<T extends readonly unknown[]> =
T extends readonly [...unknown[], infer U] ? U :
T extends readonly [unknown, ...unknown[]] ? T[number] :
T[number] | undefined;

type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, unknown] ? U : [...T];

type T00 = First<[number, symbol, string]>;
type T01 = First<[symbol, string]>;
Expand Down Expand Up @@ -350,8 +357,8 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(394,7): error TS2322: Typ
type T24 = Last<[symbol, ...string[]]>;
type T25 = Last<[string?]>;
type T26 = Last<string[]>;
type T27 = Last<[]>; // unknown, maybe should undefined
type T28 = Last<any>; // unknown, maybe should be any
type T27 = Last<[]>;
type T28 = Last<any>;
type T29 = Last<never>;

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

call('hello', 32, (a, b) => 42);

// Would be nice to infer [...string[], (...args: string[]) => number] here
// Requires [starting-fixed-part, ...rest-part, ending-fixed-part] tuple structure

call(...sa, (...x) => 42);

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

type Numbers = number[];
type Unbounded = [...Numbers, boolean];
const data: Unbounded = [false, false];
const data: Unbounded = [false, false]; // Error
~~~~
!!! error TS2322: Type '[false, false]' is not assignable to type '[...number[], boolean]'.
!!! error TS2322: Type at position 0 in source is not compatible with type at position 0 in target.
Expand Down
39 changes: 20 additions & 19 deletions tests/baselines/reference/variadicTuples1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
type TV0<T extends unknown[]> = [string, ...T];
type TV1<T extends unknown[]> = [string, ...T, number];
type TV2<T extends unknown[]> = [string, ...T, number, ...T];
type TV3<T extends unknown[]> = [string, ...T, ...number[], ...T]; // Error
type TV3<T extends unknown[]> = [string, ...T, ...number[], ...T];

// Normalization

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

// Inference between variadic tuple types

type First<T extends readonly unknown[]> = T[0];
type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T];
type First<T extends readonly unknown[]> =
T extends readonly [unknown, ...unknown[]] ? T[0] :
T[0] | undefined;

type Last<T extends readonly unknown[]> = T extends readonly [...infer _, infer U] ? U : T extends readonly [...infer _, (infer U)?] ? U | undefined : undefined;
type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?] ? U : [...T];
type DropFirst<T extends readonly unknown[]> = T extends readonly [unknown?, ...infer U] ? U : [...T];

type Last<T extends readonly unknown[]> =
T extends readonly [...unknown[], infer U] ? U :
T extends readonly [unknown, ...unknown[]] ? T[number] :
T[number] | undefined;

type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, unknown] ? U : [...T];

type T00 = First<[number, symbol, string]>;
type T01 = First<[symbol, string]>;
Expand Down Expand Up @@ -241,8 +248,8 @@ type T23 = Last<[number, symbol, ...string[]]>;
type T24 = Last<[symbol, ...string[]]>;
type T25 = Last<[string?]>;
type T26 = Last<string[]>;
type T27 = Last<[]>; // unknown, maybe should undefined
type T28 = Last<any>; // unknown, maybe should be any
type T27 = Last<[]>;
type T28 = Last<any>;
type T29 = Last<never>;

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

call('hello', 32, (a, b) => 42);

// Would be nice to infer [...string[], (...args: string[]) => number] here
// Requires [starting-fixed-part, ...rest-part, ending-fixed-part] tuple structure

call(...sa, (...x) => 42);

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

type Numbers = number[];
type Unbounded = [...Numbers, boolean];
const data: Unbounded = [false, false];
const data: Unbounded = [false, false]; // Error

type U1 = [string, ...Numbers, boolean];
type U2 = [...[string, ...Numbers], boolean];
Expand Down Expand Up @@ -588,8 +591,6 @@ ft([1, 2], [1, 2, 3]);
ft(['a', 'b'], ['c', 'd']);
ft(['a', 'b'], ['c', 'd', 42]);
call('hello', 32, function (a, b) { return 42; });
// Would be nice to infer [...string[], (...args: string[]) => number] here
// Requires [starting-fixed-part, ...rest-part, ending-fixed-part] tuple structure
call.apply(void 0, __spreadArrays(sa, [function () {
var x = [];
for (var _i = 0; _i < arguments.length; _i++) {
Expand Down Expand Up @@ -619,7 +620,7 @@ function callApi(method) {
}
callApi(getUser);
callApi(getOrgUser);
var data = [false, false];
var data = [false, false]; // Error


//// [variadicTuples1.d.ts]
Expand Down Expand Up @@ -674,10 +675,10 @@ declare function f12<T extends readonly unknown[]>(t: T, m: [...T], r: readonly
declare function f13<T extends string[], U extends T>(t0: T, t1: [...T], t2: [...U]): void;
declare function f14<T extends readonly string[], U extends T>(t0: T, t1: [...T], t2: [...U]): void;
declare function f15<T extends string[], U extends T>(k0: keyof T, k1: keyof [...T], k2: keyof [...U], k3: keyof [1, 2, ...T]): void;
declare type First<T extends readonly unknown[]> = T[0];
declare type DropFirst<T extends readonly unknown[]> = T extends readonly [any?, ...infer U] ? U : [...T];
declare type Last<T extends readonly unknown[]> = T extends readonly [...infer _, infer U] ? U : T extends readonly [...infer _, (infer U)?] ? U | undefined : undefined;
declare type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?] ? U : [...T];
declare type First<T extends readonly unknown[]> = T extends readonly [unknown, ...unknown[]] ? T[0] : T[0] | undefined;
declare type DropFirst<T extends readonly unknown[]> = T extends readonly [unknown?, ...infer U] ? U : [...T];
declare type Last<T extends readonly unknown[]> = T extends readonly [...unknown[], infer U] ? U : T extends readonly [unknown, ...unknown[]] ? T[number] : T[number] | undefined;
declare type DropLast<T extends readonly unknown[]> = T extends readonly [...infer U, unknown] ? U : [...T];
declare type T00 = First<[number, symbol, string]>;
declare type T01 = First<[symbol, string]>;
declare type T02 = First<[string]>;
Expand Down
Loading