microsoft / microsoft/TypeScript

Check inferred `this` parameter against interface requirement `this: void`

Open
#44,513 6 comments 1 reaction 1 assignee View on GitHub

@sandersn is already working on this.

Since Jun 15, 2021.

Rescheduled Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

interface method requirement this: void

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
interface Foo {
  method(this: void): number;
}
class Example implements Foo {
  x = 3;
  method() { return this.x; } // no error
}
🙁 Actual behavior

No error

🙂 Expected behavior

Error because the method implementation's implicit this: Example is not compatible with the interface requirement this: void.

The @typescript-eslint/unbound-method lint rule uses this: void as an annotation indicating the method is allowed to be used without binding this. However, there seems to be no checking from the TS compiler as to whether an inferred this type in a method implementation matches an interface requirement. Thus the lint rule can help you add this: void to the interface if you intend for functions to be usable when not bound... but neither the linter nor compiler currently help prevent runtime errors resulting from implementations with mismatched this types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.