microsoft / microsoft/TypeScript

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

Ouverte
#59,804 1 commentaire 7 réactions 1 personne assignée Voir sur GitHub

@RyanCavanaugh y travaille déjà.

Depuis le 30/8/2024.

Bug Domain: check: Control Flow
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔎 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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.