microsoft / microsoft/TypeScript

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

Aperta
#59,804 1 commento 7 reazioni 1 assegnatario Vedi su GitHub

@RyanCavanaugh ci sta già lavorando.

Dal 30/8/2024.

Bug Domain: check: Control Flow
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.