Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined is narrowed away incorrectly #60106

Open
TrippTrapp84 opened this issue Sep 30, 2024 · 3 comments Β· May be fixed by #60120
Open

undefined is narrowed away incorrectly #60106

TrippTrapp84 opened this issue Sep 30, 2024 · 3 comments Β· May be fixed by #60120
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@TrippTrapp84
Copy link

TrippTrapp84 commented Sep 30, 2024

πŸ”Ž 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

@jcalz
Copy link
Contributor

jcalz commented Sep 30, 2024

presumably the impl of #55613 let this slip through?

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Sep 30, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 30, 2024
@Andarist
Copy link
Contributor

It's caused by #53714 . I'm investigating this since it's my PR

@Andarist
Copy link
Contributor

Andarist commented Oct 1, 2024

I think there were some pre-existing inconsistencies in how optional chains might be narrowed down and how negation impacts them. I continue to investigate this issue and the other ones I found here: TS playground 1, TS playground 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants