microsoft / microsoft/TypeScript

Compiler warning for always true/false strict inequality expression

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

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

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

説明

Search Terms

type guard warning if always true false non-identity inequality

Suggestion

The compiler should emit a warning if it can determine that an expression using a strict inequality check (!==) always returns true or false. It already does this for strict equality checks (===).

Use Cases

Like the warnings for the strict equality check, it helps identifying unreachable code blocks or unnecessary if blocks which can get introduced during refactoring of existing code or during carefree creation of new code.

Examples

The following function definition emits an error ("This condition will always return 'false' since the types '"abc"' and '"xyz"' have no overlap."):

function fn(p: "abc") {
  if (p === "xyz") {
    
  }
}

But this function definition compiles just fine (a possible error message could be "This condition will always return 'true' since the types '"abc"' and '"abc"' overlap completely":

function fn2(p: "abc") {
  if (p !== "abc") {
    
  }
}

It even correctly type-guards the type of p inside the if block (it becomes never).

Two other scenarios which trigger this non-warning, but could be harder to detect:

// typeof checks
function fn3(p: string) {
  if (typeof p !== "string") {

  }
}

// the length of a tuple
function fn4(p: []) {
  if (p.length !== 0) {

  }
}

The last example doesn't type-guard the value of p correctly to never even though p.length is correctly of type 0, maybe this is a separate feature request.

Problems / Discussion Points

  • I'm not sure whether this qualifies as a breaking change, since it introduces a warning for code constructs which compile totally fine at the moment.
  • This could be extended for other checks as well (e.g. for the </<= and >/>= operators) even though this would be pretty edge-casey.

Checklist

My suggestion meets these guidelines:

  • (Unsure) This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

ファイル、テスト、コンパイラのエントリポイントは指定されていません。まず、厳密等価チェックに対する既存の診断を見つけ、厳密不等価、typeof、タプル長の式がどのように解析されているかを比較してください。常に真または常に偽であることが証明可能な !== 条件に対する診断を追加し、出力される JavaScript を変更しなければ完了です。

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

評価

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

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

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