Skip to content

Infer implied arity before inferring from 'this' argument #39328

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 2 commits into from
Jun 30, 2020
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
15 changes: 8 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25773,13 +25773,6 @@ namespace ts {
}
}

const thisType = getThisTypeOfSignature(signature);
if (thisType) {
const thisArgumentNode = getThisArgumentOfCall(node);
const thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType;
inferTypes(context.inferences, thisArgumentType, thisType);
}

const restType = getNonArrayRestType(signature);
const argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
if (restType && restType.flags & TypeFlags.TypeParameter) {
Expand All @@ -25788,6 +25781,14 @@ namespace ts {
info.impliedArity = findIndex(args, isSpreadArgument, argCount) < 0 ? args.length - argCount : undefined;
}
}

const thisType = getThisTypeOfSignature(signature);
if (thisType) {
const thisArgumentNode = getThisArgumentOfCall(node);
const thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType;
inferTypes(context.inferences, thisArgumentType, thisType);
}

for (let i = 0; i < argCount; i++) {
const arg = args[i];
if (arg.kind !== SyntaxKind.OmittedExpression) {
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/variadicTuples1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,14 @@ tests/cases/conformance/types/tuple/variadicTuples1.ts(342,19): error TS2322: Ty
let v2 = f22(["foo", "bar"]); // [string, string]
let v3 = f22(["foo", 42]); // [string]
}

// Repro from #39327

interface Desc<A extends unknown[], T> {
readonly f: (...args: A) => T;
bind<T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T): Desc<[...U], R>;
}

declare const a: Desc<[string, number, boolean], object>;
const b = a.bind("", 1); // Desc<[boolean], object>

17 changes: 17 additions & 0 deletions tests/baselines/reference/variadicTuples1.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ function f23<U extends string[]>(args: [...U, number]) {
let v2 = f22(["foo", "bar"]); // [string, string]
let v3 = f22(["foo", 42]); // [string]
}

// Repro from #39327

interface Desc<A extends unknown[], T> {
readonly f: (...args: A) => T;
bind<T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T): Desc<[...U], R>;
}

declare const a: Desc<[string, number, boolean], object>;
const b = a.bind("", 1); // Desc<[boolean], object>


//// [variadicTuples1.js]
Expand Down Expand Up @@ -557,6 +567,7 @@ function f23(args) {
var v2 = f22(["foo", "bar"]); // [string, string]
var v3 = f22(["foo", 42]); // [string]
}
var b = a.bind("", 1); // Desc<[boolean], object>


//// [variadicTuples1.d.ts]
Expand Down Expand Up @@ -703,3 +714,9 @@ declare function f21<U extends string[]>(args: [...U, number?]): void;
declare function f22<T extends unknown[] = []>(args: [...T, number]): T;
declare function f22<T extends unknown[] = []>(args: [...T]): T;
declare function f23<U extends string[]>(args: [...U, number]): void;
interface Desc<A extends unknown[], T> {
readonly f: (...args: A) => T;
bind<T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T): Desc<[...U], R>;
}
declare const a: Desc<[string, number, boolean], object>;
declare const b: Desc<[boolean], object>;
40 changes: 40 additions & 0 deletions tests/baselines/reference/variadicTuples1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -1204,3 +1204,43 @@ function f23<U extends string[]>(args: [...U, number]) {
>f22 : Symbol(f22, Decl(variadicTuples1.ts, 342, 1), Decl(variadicTuples1.ts, 344, 72))
}

// Repro from #39327

interface Desc<A extends unknown[], T> {
>Desc : Symbol(Desc, Decl(variadicTuples1.ts, 351, 1))
>A : Symbol(A, Decl(variadicTuples1.ts, 355, 15))
>T : Symbol(T, Decl(variadicTuples1.ts, 355, 35))

readonly f: (...args: A) => T;
>f : Symbol(Desc.f, Decl(variadicTuples1.ts, 355, 40))
>args : Symbol(args, Decl(variadicTuples1.ts, 356, 17))
>A : Symbol(A, Decl(variadicTuples1.ts, 355, 15))
>T : Symbol(T, Decl(variadicTuples1.ts, 355, 35))

bind<T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T): Desc<[...U], R>;
>bind : Symbol(Desc.bind, Decl(variadicTuples1.ts, 356, 34))
>T : Symbol(T, Decl(variadicTuples1.ts, 357, 9))
>U : Symbol(U, Decl(variadicTuples1.ts, 357, 29))
>R : Symbol(R, Decl(variadicTuples1.ts, 357, 50))
>this : Symbol(this, Decl(variadicTuples1.ts, 357, 54))
>Desc : Symbol(Desc, Decl(variadicTuples1.ts, 351, 1))
>T : Symbol(T, Decl(variadicTuples1.ts, 357, 9))
>U : Symbol(U, Decl(variadicTuples1.ts, 357, 29))
>R : Symbol(R, Decl(variadicTuples1.ts, 357, 50))
>args : Symbol(args, Decl(variadicTuples1.ts, 357, 82))
>T : Symbol(T, Decl(variadicTuples1.ts, 357, 9))
>Desc : Symbol(Desc, Decl(variadicTuples1.ts, 351, 1))
>U : Symbol(U, Decl(variadicTuples1.ts, 357, 29))
>R : Symbol(R, Decl(variadicTuples1.ts, 357, 50))
}

declare const a: Desc<[string, number, boolean], object>;
>a : Symbol(a, Decl(variadicTuples1.ts, 360, 13))
>Desc : Symbol(Desc, Decl(variadicTuples1.ts, 351, 1))

const b = a.bind("", 1); // Desc<[boolean], object>
>b : Symbol(b, Decl(variadicTuples1.ts, 361, 5))
>a.bind : Symbol(Desc.bind, Decl(variadicTuples1.ts, 356, 34))
>a : Symbol(a, Decl(variadicTuples1.ts, 360, 13))
>bind : Symbol(Desc.bind, Decl(variadicTuples1.ts, 356, 34))

25 changes: 25 additions & 0 deletions tests/baselines/reference/variadicTuples1.types
Original file line number Diff line number Diff line change
Expand Up @@ -1254,3 +1254,28 @@ function f23<U extends string[]>(args: [...U, number]) {
>42 : 42
}

// Repro from #39327

interface Desc<A extends unknown[], T> {
readonly f: (...args: A) => T;
>f : (...args: A) => T
>args : A

bind<T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T): Desc<[...U], R>;
>bind : <T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T) => Desc<[...U], R>
>this : Desc<[...T, ...U], R>
>args : T
}

declare const a: Desc<[string, number, boolean], object>;
>a : Desc<[string, number, boolean], object>

const b = a.bind("", 1); // Desc<[boolean], object>
>b : Desc<[boolean], object>
>a.bind("", 1) : Desc<[boolean], object>
>a.bind : <T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T) => Desc<[...U], R>
>a : Desc<[string, number, boolean], object>
>bind : <T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T) => Desc<[...U], R>
>"" : ""
>1 : 1

10 changes: 10 additions & 0 deletions tests/cases/conformance/types/tuple/variadicTuples1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,13 @@ function f23<U extends string[]>(args: [...U, number]) {
let v2 = f22(["foo", "bar"]); // [string, string]
let v3 = f22(["foo", 42]); // [string]
}

// Repro from #39327

interface Desc<A extends unknown[], T> {
readonly f: (...args: A) => T;
bind<T extends unknown[], U extends unknown[], R>(this: Desc<[...T, ...U], R>, ...args: T): Desc<[...U], R>;
}

declare const a: Desc<[string, number, boolean], object>;
const b = a.bind("", 1); // Desc<[boolean], object>