Skip to content

Fix contextual types computed from rest parameters #29858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 10 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21550,32 +21550,31 @@ namespace ts {
}
if (signature.hasRestParameter) {
const restType = getTypeOfSymbol(signature.parameters[paramCount]);
if (isTupleType(restType)) {
if (pos - paramCount < getLengthOfTupleType(restType)) {
return restType.typeArguments![pos - paramCount];
}
return getRestTypeOfTupleType(restType);
}
return getIndexTypeOfType(restType, IndexKind.Number);
const indexType = getLiteralType(pos - paramCount);
return getIndexedAccessType(restType, indexType);
}
return undefined;
}

function getRestTypeAtPosition(source: Signature, pos: number): Type {
const paramCount = getParameterCount(source);
const restType = getEffectiveRestType(source);
if (restType && pos === paramCount - 1) {
const nonRestCount = paramCount - (restType ? 1 : 0);
if (restType && pos === nonRestCount) {
return restType;
}
const start = restType ? Math.min(pos, paramCount - 1) : pos;
const types = [];
const names = [];
for (let i = start; i < paramCount; i++) {
for (let i = pos; i < nonRestCount; i++) {
types.push(getTypeAtPosition(source, i));
names.push(getParameterNameAtPosition(source, i));
}
if (restType) {
types.push(getIndexedAccessType(restType, numberType));
names.push(getParameterNameAtPosition(source, nonRestCount));
}
const minArgumentCount = getMinArgumentCount(source);
const minLength = minArgumentCount < start ? 0 : minArgumentCount - start;
const minLength = minArgumentCount < pos ? 0 : minArgumentCount - pos;
return createTupleType(types, minLength, !!restType, /*readonly*/ false, names);
}

Expand Down
26 changes: 20 additions & 6 deletions tests/baselines/reference/restTuplesFromContextualTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error TS2345: Argument of type '(a: number, b: any, ...x: any[]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error TS2345: Argument of type '(a: number, b: T[0], ...x: T[number][]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
Types of parameters 'b' and 'args' are incompatible.
Type 'T' is not assignable to type '[any, ...any[]]'.
Property '0' is missing in type 'any[]' but required in type '[any, ...any[]]'.
Type 'T' is not assignable to type '[T[0], ...T[number][]]'.
Property '0' is missing in type 'any[]' but required in type '[T[0], ...T[number][]]'.


==== tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts (1 errors) ====
Expand Down Expand Up @@ -62,10 +62,10 @@ tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error
f((a, ...x) => {});
f((a, b, ...x) => {});
~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(a: number, b: any, ...x: any[]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
!!! error TS2345: Argument of type '(a: number, b: T[0], ...x: T[number][]) => void' is not assignable to parameter of type '(x: number, ...args: T) => void'.
!!! error TS2345: Types of parameters 'b' and 'args' are incompatible.
!!! error TS2345: Type 'T' is not assignable to type '[any, ...any[]]'.
!!! error TS2345: Property '0' is missing in type 'any[]' but required in type '[any, ...any[]]'.
!!! error TS2345: Type 'T' is not assignable to type '[T[0], ...T[number][]]'.
!!! error TS2345: Property '0' is missing in type 'any[]' but required in type '[T[0], ...T[number][]]'.
}

// Repro from #25288
Expand All @@ -79,4 +79,18 @@ tests/cases/conformance/types/rest/restTuplesFromContextualTypes.ts(56,7): error

(function foo(...rest){}(1, ''));
take(function(...rest){});

// Repro from #29833

type ArgsUnion = [number, string] | [number, Error];
type TupleUnionFunc = (...params: ArgsUnion) => number;

const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
return num;
};

const funcUnionTupleRest: TupleUnionFunc = (...params) => {
const [num, strOrErr] = params;
return num;
};

29 changes: 29 additions & 0 deletions tests/baselines/reference/restTuplesFromContextualTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ declare function take(cb: (a: number, b: string) => void): void;

(function foo(...rest){}(1, ''));
take(function(...rest){});

// Repro from #29833

type ArgsUnion = [number, string] | [number, Error];
type TupleUnionFunc = (...params: ArgsUnion) => number;

const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
return num;
};

const funcUnionTupleRest: TupleUnionFunc = (...params) => {
const [num, strOrErr] = params;
return num;
};


//// [restTuplesFromContextualTypes.js]
Expand Down Expand Up @@ -274,6 +288,17 @@ take(function () {
rest[_i] = arguments[_i];
}
});
var funcUnionTupleNoRest = function (num, strOrErr) {
return num;
};
var funcUnionTupleRest = function () {
var params = [];
for (var _i = 0; _i < arguments.length; _i++) {
params[_i] = arguments[_i];
}
var num = params[0], strOrErr = params[1];
return num;
};


//// [restTuplesFromContextualTypes.d.ts]
Expand All @@ -286,3 +311,7 @@ declare function f3(cb: (x: number, ...args: typeof t3) => void): void;
declare function f4<T extends any[]>(t: T): void;
declare var tuple: [number, string];
declare function take(cb: (a: number, b: string) => void): void;
declare type ArgsUnion = [number, string] | [number, Error];
declare type TupleUnionFunc = (...params: ArgsUnion) => number;
declare const funcUnionTupleNoRest: TupleUnionFunc;
declare const funcUnionTupleRest: TupleUnionFunc;
37 changes: 37 additions & 0 deletions tests/baselines/reference/restTuplesFromContextualTypes.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,40 @@ take(function(...rest){});
>take : Symbol(take, Decl(restTuplesFromContextualTypes.ts, 61, 33))
>rest : Symbol(rest, Decl(restTuplesFromContextualTypes.ts, 68, 14))

// Repro from #29833

type ArgsUnion = [number, string] | [number, Error];
>ArgsUnion : Symbol(ArgsUnion, Decl(restTuplesFromContextualTypes.ts, 68, 26))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

type TupleUnionFunc = (...params: ArgsUnion) => number;
>TupleUnionFunc : Symbol(TupleUnionFunc, Decl(restTuplesFromContextualTypes.ts, 72, 52))
>params : Symbol(params, Decl(restTuplesFromContextualTypes.ts, 73, 23))
>ArgsUnion : Symbol(ArgsUnion, Decl(restTuplesFromContextualTypes.ts, 68, 26))

const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
>funcUnionTupleNoRest : Symbol(funcUnionTupleNoRest, Decl(restTuplesFromContextualTypes.ts, 75, 5))
>TupleUnionFunc : Symbol(TupleUnionFunc, Decl(restTuplesFromContextualTypes.ts, 72, 52))
>num : Symbol(num, Decl(restTuplesFromContextualTypes.ts, 75, 46))
>strOrErr : Symbol(strOrErr, Decl(restTuplesFromContextualTypes.ts, 75, 50))

return num;
>num : Symbol(num, Decl(restTuplesFromContextualTypes.ts, 75, 46))

};

const funcUnionTupleRest: TupleUnionFunc = (...params) => {
>funcUnionTupleRest : Symbol(funcUnionTupleRest, Decl(restTuplesFromContextualTypes.ts, 79, 5))
>TupleUnionFunc : Symbol(TupleUnionFunc, Decl(restTuplesFromContextualTypes.ts, 72, 52))
>params : Symbol(params, Decl(restTuplesFromContextualTypes.ts, 79, 44))

const [num, strOrErr] = params;
>num : Symbol(num, Decl(restTuplesFromContextualTypes.ts, 80, 9))
>strOrErr : Symbol(strOrErr, Decl(restTuplesFromContextualTypes.ts, 80, 13))
>params : Symbol(params, Decl(restTuplesFromContextualTypes.ts, 79, 44))

return num;
>num : Symbol(num, Decl(restTuplesFromContextualTypes.ts, 80, 9))

};

45 changes: 40 additions & 5 deletions tests/baselines/reference/restTuplesFromContextualTypes.types
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ function f4<T extends any[]>(t: T) {
f((...x) => {});
>f((...x) => {}) : void
>f : (cb: (x: number, ...args: T) => void) => void
>(...x) => {} : (x: number, ...args: any[]) => void
>x : [number, ...any[]]
>(...x) => {} : (x: number, ...args: T[number][]) => void
>x : [number, ...T[number][]]

f((a, ...x) => {});
>f((a, ...x) => {}) : void
Expand All @@ -345,10 +345,10 @@ function f4<T extends any[]>(t: T) {
f((a, b, ...x) => {});
>f((a, b, ...x) => {}) : void
>f : (cb: (x: number, ...args: T) => void) => void
>(a, b, ...x) => {} : (a: number, b: any, ...x: any[]) => void
>(a, b, ...x) => {} : (a: number, b: T[0], ...x: T[number][]) => void
>a : number
>b : any
>x : any[]
>b : T[0]
>x : T[number][]
}

// Repro from #25288
Expand Down Expand Up @@ -389,3 +389,38 @@ take(function(...rest){});
>function(...rest){} : (a: number, b: string) => void
>rest : [number, string]

// Repro from #29833

type ArgsUnion = [number, string] | [number, Error];
>ArgsUnion : ArgsUnion

type TupleUnionFunc = (...params: ArgsUnion) => number;
>TupleUnionFunc : TupleUnionFunc
>params : ArgsUnion

const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
>funcUnionTupleNoRest : TupleUnionFunc
>(num, strOrErr) => { return num;} : (num: number, strOrErr: string | Error) => number
>num : number
>strOrErr : string | Error

return num;
>num : number

};

const funcUnionTupleRest: TupleUnionFunc = (...params) => {
>funcUnionTupleRest : TupleUnionFunc
>(...params) => { const [num, strOrErr] = params; return num;} : (...params: ArgsUnion) => number
>params : ArgsUnion

const [num, strOrErr] = params;
>num : number
>strOrErr : string | Error
>params : ArgsUnion

return num;
>num : number

};

Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ declare function take(cb: (a: number, b: string) => void): void;

(function foo(...rest){}(1, ''));
take(function(...rest){});

// Repro from #29833

type ArgsUnion = [number, string] | [number, Error];
type TupleUnionFunc = (...params: ArgsUnion) => number;

const funcUnionTupleNoRest: TupleUnionFunc = (num, strOrErr) => {
return num;
};

const funcUnionTupleRest: TupleUnionFunc = (...params) => {
const [num, strOrErr] = params;
return num;
};