Microsoft/TypeScript

undefined is narrowed away incorrectly

Open

#60,106 建立於 2024年9月30日

在 GitHub 查看
 (3 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: check: Control FlowHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

🔎 Search Terms

narrowing undefined, incorrect type narrowing, incorrect if statement narrowing

🕗 Version & Regression Information

  • This changed between versions 5.2 and 5.3

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEkoGdnwILwN4CgCQAksugBQCUAXPAC4AWAlmoxgNw4C+OOoSciA9gDtk1eAAco1aiBiCqmAD7wAroNAAzeoJDA261WGr0h8KGLEQAngAVJ02eWz566+CQlSZggPwA6IqRk8AC8ofDqUBDIIEG4eHge9oJs8QD0qfAAet74eHDUyrIpnAl2XinpWTnsQA

💻 Code

declare class A {
	IsA(): this is A;
}

declare const pattern: A | undefined;
function applyPattern() {
	if (pattern?.IsA() === false) {
		pattern;
		// ^?
		return;
	}
	pattern;
	// ^?
}

🙁 Actual behavior

pattern is narrowed down to A, removing the undefined

🙂 Expected behavior

pattern should only be narrowed to A | undefined

Additional information about the issue

No response

貢獻者指南