Microsoft/TypeScript

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

Open

#52,439 opened on Jan 26, 2023

View on GitHub
ย (1 comment)ย (1 reaction)ย (0 assignees)TypeScriptย (48,455 stars)ย (6,726 forks)batch import
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

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

Non-null assertion should work on possibly-uninitialized auto-typed `let` variables ยท Microsoft/TypeScript#52439 | Good First Issue