Microsoft/TypeScript

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

Open

#52.439 geöffnet am 26. Jan. 2023

Auf GitHub ansehen
 (1 Kommentar) (1 Reaktion) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Experience EnhancementHelp WantedSuggestion

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

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

Contributor Guide