microsoft / microsoft/TypeScript

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

オープン
#44,513 コメント 6 件 リアクション 1 件 担当者 1 名 GitHub で見る

@sandersn がすでに取り組んでいます。

2021年6月15日 から。

Rescheduled Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。