microsoft / microsoft/TypeScript

Flag 'instanceof' expressions that are provably always true or false

オープン
#32,801 コメント 6 件 リアクション 4 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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

説明

Suggestion

When refactoring code, TypeScript is generally very good at finding and reporting situations where existing code will break due to type changes. This allows "refactoring with confidence". This isn't surprising given that one of the primary goals of TypeScript is to "statically identify constructs that are likely to be errors".

There's one common case where TypeScript is "blind" and doesn't report errors when types are refactored. This case involves the use of the 'instanceof' operator. This operator can be used in situations where it makes no sense -- e.g. where the type specified on the LHS has no possible relation to the type specified on the RHS.

To help developers avoid this common programming error, I propose that TypeScript report an error for any 'instanceof' expression that is provably always true or false at compilation time. Any such expression will likely be unintended by the programmer and should be flagged as errors. At best, such operations represent unnecessary code that imposes runtime overhead.

Example

// ClassB is a subclass of ClassA.
class ClassA {}
class ClassB extends ClassA {}

// ClassC has nothing to do with either
// ClassA or ClassB.
class ClassC {
    method1() {}
}

function function1(param1: ClassB) {
    // Static analysis can prove this expression
    // will always evaluate to false. It's a common
    // source of programming errors, especially during
    // refactoring.
    if (param1 instanceof ClassC) {
        // This call isn't even valid given that param1
        // was declared as type ClassB.
        param1.method1();
    }

    // Static analysis can prove this expression will
    // always evaluate to true, so at best it's unnecessary
    // runtime overhead and more commonly is a behavior
    // that is unintended by the programmer.
    if (param1 instanceof ClassA) {
        // ...
    }
}

Are there any legitimate uses of 'instanceof' where the result is statically provable to be true or false in all cases? I can't think of any, but it's possible I'm overlooking some specialized cases. If there are, this check could be added as an optional compiler switch to preserve the current behavior.

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

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

はじめの一歩

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

調査の方向性

まず、issue の ClassA、ClassB、ClassC の例と、TypeScript コンパイラーにおける既存の instanceof 型チェック動作を確認します。常に true になるケースと常に false になるケースを、正当な例外も含めてどのように診断すべきかを判断します。コンパイラーが意図したケースを報告し、例についてのカバレッジがあり、有効な instanceof の使用が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
28/100

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

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