Microsoft/TypeScript
View on GitHubNon-null assertion should work on possibly-uninitialized auto-typed `let` variables
Open
#52,439 opened on Jan 26, 2023
Experience EnhancementHelp WantedSuggestion
Description
Bug Report
๐ Search Terms
non-null postfix bang implicit any
๐ Version & Regression Information
- This is the behavior in every version I tried
โฏ Playground 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