microsoft / microsoft/TypeScript

Variables that are used-before-assigned are typed without `undefined`

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

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

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

説明

Bug Report

🔎 Search Terms

variable use before assign

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
let xx: number;

  xx.toFixed();
// ^?
// typeof xx === number ❌

let yy!: number;

  yy.toFixed();
// ^?
// typeof yy === number ✅
🙁 Actual behavior

TS reports the type of the used-before-assigned variable xx as number, and reports error Variable 'xx' is used before being assigned. (2454)

🙂 Expected behavior

TS reports the type of xx as number | undefined.

Additional info

This behaviour makes it difficult for us to write lint rules because when we attempt to check if the variable is undefined, the type system reports that it is not.

Examples:

  • checking for unnecessary conditions based on types
    • if (xx != null) {} The types say xx is number, so != null looks like it's unnecessary.
  • checking for unnecessary type assertions
    • xx! Again the types say xx is number, so the non-null assertion looks like it's unnecessary.

Example issue:

In the past we've manually coded up logic to do used-before-assigned checks where necessary - but it's pretty gnarly code to maintain and we have to ensure that we cover this case in our implementation.

It would be great if TS reported the "correct" type here with undefined.

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

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

はじめの一歩

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

調査の方向性

リンクされている TypeScript Playground から始め、let xx: numberlet yy!: number の違いを再現してください。代入前使用の解析が報告される型にどのような影響を与えるかを追跡し、issue にリンクされている既存の typescript-eslint の処理と比較してください。完了条件は、診断や non-null アサーションされたケースを壊すことなく、最初の変数が number | undefined として公開されることです。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
30/100

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

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