Microsoft/TypeScript

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

Open

#52,439 创建于 2023年1月26日

在 GitHub 查看
 (1 评论) (1 反应) (0 负责人)TypeScript (6,726 fork)batch import
Experience EnhancementHelp WantedSuggestion

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

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

贡献者指南