-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type of object property isn’t narrowed when captured by closure #51490
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
Comments
This is indeed a duplicate of #9998; narrowing doesn’t propagate across function boundaries. I think it only works for |
Hi @fatcerberus, First, thanks for the quick reply.
Fair point. Could the fact that the function wouldn’t even come into existence (because of the
Can this control-flow analysis be adapted to work with objects, or is there some underlying limitation that would prevent that? Thanks. |
Both points are discussed in #9998, and the TypeScript team is aware of this limitation. |
@MartinJohns Sounds good. Thanks for your answer. |
Bug Report
🔎 Search Terms
“TypeScript narrowing of object field captured in closure”
I found #35124 and #9998 but they aren’t quite the same thing.
In my case, it’s an issue that only occurs with object properties, not bare variables like in #35124.
Also, my version is relying on
return
to avoid creating the closure that would have captured the variable in the first place.🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Type of object property isn’t narrowed when captured by closure, like a bare variable is.
The only workaround I could find is the check for the condition again in the closure:
🙂 Expected behavior
Type of object property should be narrowed when captured by closure, like a bare variable is.
The text was updated successfully, but these errors were encountered: