Skip to content

Generic type inference does not appear to fill in type of this parameter #10072

Closed
@ghost

Description

TypeScript Version: nightly

Code

function f<T>(x: T, f: (x: T) => string): void {}
f("", x => x);

function g<T>(x: T, f: (this: T) => string): void {}
g<string>("", function() { return this; });
g("", function() { return this; });

Expected behavior:

No error. this is always of type string.

Actual behavior:

In the last line, the type of this is still T; it hasn't been 'filled in'.

src/a.ts(6,7): error TS2345: Argument of type '() => T' is not assignable to parameter of type '(this: string) => string'.
  Type 'T' is not assignable to type 'string'.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already created

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions