microsoft / microsoft/TypeScript
Check inferred `this` parameter against interface requirement `this: void`
@sandersn arbeitet bereits daran.
Seit 15.6.2021.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.