microsoft / microsoft/TypeScript

TS2454 Variable is used before being assigned with strictNullChecks when variable is clearly assigned but can be undefined

オープン
#59,804 コメント 1 件 リアクション 7 件 担当者 1 名 GitHub で見る

@RyanCavanaugh がすでに取り組んでいます。

2024年8月30日 から。

Bug Domain: check: Control Flow
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

Variable is used before being assigned TS2454 TS(2454) undefined strictNullChecks

🕗 Version & Regression Information
  • This happens at least since version 3.7.5 (tested in the playground)
  • This is the behavior in every version I tried, and I reviewed the FAQ
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/DYUwLgBGIM6QvBArgOwCYgGYEsUjQNwBQR0cEiA5JRAIYwRwBOuA5hAD7Lpa77GlYYAPwA6UClZgAFgSA

💻 Code
let test = undefined;

test = '' as string | undefined;

test?.length; // Variable 'test' is used before being assigned.
🙁 Actual behavior

Typescript throws an error complaining about test being used before being assigned but it's clearly assigned to undefined and then to ''.

Mine is a very simplicistic problem, but the same issues happens if a replace '' with a call to a function.

🙂 Expected behavior

Typescript should assign the type string | undefined to the variable test and let me use it.

Additional information about the issue

No response

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

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

はじめの一歩

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

評価

この issue はまだ評価されていません。

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

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