Microsoft/TypeScript

Non-null assertion should work on possibly-uninitialized auto-typed `let` variables

Open

#52.439 aperta il 26 gen 2023

Vedi su GitHub
 (1 commento) (1 reazione) (0 assegnatari)TypeScript (6726 fork)batch import
Experience EnhancementHelp WantedSuggestion

Metriche repository

Star
 (48.455 star)
Metriche merge PR
 (Merge medio 6g 17h) (9 PR mergiate in 30 g)

Descrizione

Bug Report

🔎 Search Terms

non-null postfix bang implicit any

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

This is the link

💻 Code

let foo;
if (Math.random() > 0.5) {
  foo = "hello";
}
// bar: string | undefined, seems fine
let bar = foo;
/* -- pretend I know foo is defined for other reasons -- */
// Error, foo is implicitly any
let baz = foo!;

🙁 Actual behavior

Implicit any on foo!

🙂 Expected behavior

Since the inferred type of foo (as seen in bar) is string | undefined, foo! should trivially have the type string

Guida contributor