Skip to content

Optional Chaining does not work with generic this parameterΒ #42404

@osyrisrblx

Description

@osyrisrblx

Bug Report

πŸ”Ž Search Terms

optional chain, generic, this, method

πŸ•— Version & Regression Information

Bug started in TypeScript 3.7.

  • I was unable to test this on prior versions because optional chaining did not exist before 3.7

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface X {
    foo<T>(this: T, arg: keyof T): void;
}

interface Y extends X {
    a: number;
    b: string;
}

declare const value: Y | undefined;

// this works
if (value) {
    value?.foo("a");
}

// this doesn't
value?.foo("a"); // Argument of type 'string' is not assignable to parameter of type 'never'.

πŸ™ Actual behavior

Generic T is not resolved. keyof T results in never.

πŸ™‚ Expected behavior

Generic T should resolve to Y so that keyof T resolves to "a" | "b" | "foo".

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions