microsoft / microsoft/TypeScript
Variables that are used-before-assigned are typed without `undefined`
まだ誰も着手していません。
- 主要言語
- 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 sayxxisnumber, so!= nulllooks like it's unnecessary.
- checking for unnecessary type assertions
xx!Again the types sayxxisnumber, so the non-null assertion looks like it's unnecessary.
Example issue:
- https://github.com/typescript-eslint/typescript-eslint/issues/453
- https://github.com/typescript-eslint/typescript-eslint/issues/6640
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている TypeScript Playground から始め、let xx: number と let yy!: number の違いを再現してください。代入前使用の解析が報告される型にどのような影響を与えるかを追跡し、issue にリンクされている既存の typescript-eslint の処理と比較してください。完了条件は、診断や non-null アサーションされたケースを壊すことなく、最初の変数が number | undefined として公開されることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 30/100