microsoft / microsoft/TypeScript

strictNullCheck should detect unreachable default values and code after nullish operator

オープン
#43,040 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る

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

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

説明

Suggestion

Using strictNullCheck should allow detection of code that isn't supposed to be reached according to type analysis

🔍 Search Terms

strictNullCheck unreachble nullish default value

✅ Viability Checklist

My suggestion meets these guidelines:

  • 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Yes, especially "Statically identify constructs that are likely to be errors."

⭐ Suggestion

This assumes that it is desired to detect errors with unreachable code due to type analysis (see microsoft/TypeScript-Website#1604 ).

With strictNullCheck turned on, some code that is meant to be unreachable and thus an error is not flagged.

📃 Motivating Example

The = 42 below is unreachable (playground) and is the sign that some code is likely to be an error, but nothing is flagged

class Example {
  foo({
    x = 42
  }: {
    x: number
  }) {
    return x
  }
}

Either the declaration should be changed to x?: number, or the default should be removed.

Another example (thanks to @jcalz) using the nullish operator.

function foo(x: number) { x ??= 42 }

Again, it would be helpful if an error or a warning would be raised as that code seems clearly to be a mistake.

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

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

はじめの一歩

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

調査の方向性

まず、strictNullCheck を有効にして、動機となった 2 つの例を再現します。デストラクチャリングされた x = 42 のデフォルト値と x ??= 42 です。到達不能なデフォルト値と nullish 代入に対して意図された診断を特定し、その後、既存の TypeScript コンパイラーテストスイートを使って、両方のケースと完了状態での動作を検証します。

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

評価

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

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

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