Skip to content

Fix ThisParameterType<T> type #36013

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
Jan 6, 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
2 changes: 1 addition & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ declare var Function: FunctionConstructor;
/**
* Extracts the type of the 'this' parameter of a function type, or 'unknown' if the function type has no 'this' parameter.
*/
type ThisParameterType<T> = T extends (this: unknown, ...args: any[]) => any ? unknown : T extends (this: infer U, ...args: any[]) => any ? U : unknown;
type ThisParameterType<T> = T extends (this: infer U, ...args: any[]) => any ? U : unknown;

/**
* Removes the 'this' parameter from a function type.
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/strictBindCallApply2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [strictBindCallApply2.ts]
// Repro from #32964

interface Foo { blub: string };
function fn(this: Foo) {}

type Test = ThisParameterType<typeof fn>;

const fb = fn.bind({ blub: "blub" });


//// [strictBindCallApply2.js]
// Repro from #32964
;
function fn() { }
var fb = fn.bind({ blub: "blub" });
24 changes: 24 additions & 0 deletions tests/baselines/reference/strictBindCallApply2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/conformance/functions/strictBindCallApply2.ts ===
// Repro from #32964

interface Foo { blub: string };
>Foo : Symbol(Foo, Decl(strictBindCallApply2.ts, 0, 0))
>blub : Symbol(Foo.blub, Decl(strictBindCallApply2.ts, 2, 15))

function fn(this: Foo) {}
>fn : Symbol(fn, Decl(strictBindCallApply2.ts, 2, 31))
>this : Symbol(this, Decl(strictBindCallApply2.ts, 3, 12))
>Foo : Symbol(Foo, Decl(strictBindCallApply2.ts, 0, 0))

type Test = ThisParameterType<typeof fn>;
>Test : Symbol(Test, Decl(strictBindCallApply2.ts, 3, 25))
>ThisParameterType : Symbol(ThisParameterType, Decl(lib.es5.d.ts, --, --))
>fn : Symbol(fn, Decl(strictBindCallApply2.ts, 2, 31))

const fb = fn.bind({ blub: "blub" });
>fb : Symbol(fb, Decl(strictBindCallApply2.ts, 7, 5))
>fn.bind : Symbol(CallableFunction.bind, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --) ... and 1 more)
>fn : Symbol(fn, Decl(strictBindCallApply2.ts, 2, 31))
>bind : Symbol(CallableFunction.bind, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --) ... and 1 more)
>blub : Symbol(blub, Decl(strictBindCallApply2.ts, 7, 20))

24 changes: 24 additions & 0 deletions tests/baselines/reference/strictBindCallApply2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/conformance/functions/strictBindCallApply2.ts ===
// Repro from #32964

interface Foo { blub: string };
>blub : string

function fn(this: Foo) {}
>fn : (this: Foo) => void
>this : Foo

type Test = ThisParameterType<typeof fn>;
>Test : Foo
>fn : (this: Foo) => void

const fb = fn.bind({ blub: "blub" });
>fb : () => void
>fn.bind({ blub: "blub" }) : () => void
>fn.bind : { <T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>; <T, A0, A extends any[], R>(this: (this: T, arg0: A0, ...args: A) => R, thisArg: T, arg0: A0): (...args: A) => R; <T, A0, A1, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1): (...args: A) => R; <T, A0, A1, A2, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1, arg2: A2): (...args: A) => R; <T, A0, A1, A2, A3, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3): (...args: A) => R; <T, AX, R>(this: (this: T, ...args: AX[]) => R, thisArg: T, ...args: AX[]): (...args: AX[]) => R; }
>fn : (this: Foo) => void
>bind : { <T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>; <T, A0, A extends any[], R>(this: (this: T, arg0: A0, ...args: A) => R, thisArg: T, arg0: A0): (...args: A) => R; <T, A0, A1, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1): (...args: A) => R; <T, A0, A1, A2, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, arg2: A2, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1, arg2: A2): (...args: A) => R; <T, A0, A1, A2, A3, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1, arg2: A2, arg3: A3): (...args: A) => R; <T, AX, R>(this: (this: T, ...args: AX[]) => R, thisArg: T, ...args: AX[]): (...args: AX[]) => R; }
>{ blub: "blub" } : { blub: string; }
>blub : string
>"blub" : "blub"

11 changes: 11 additions & 0 deletions tests/cases/conformance/functions/strictBindCallApply2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @strictFunctionTypes: false
// @strictBindCallApply: true

// Repro from #32964

interface Foo { blub: string };
function fn(this: Foo) {}

type Test = ThisParameterType<typeof fn>;

const fb = fn.bind({ blub: "blub" });