-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Missing type narrowing based on control flow analysis of logical expressions #50739
Comments
Based on #32695 I'm pretty sure that this is a design limitation and is working as expected, specifically, this rule:
The See also issues like #47634 (or other issues that link back to #32695). |
This is unfortunate. In our case we have tons of checks of the form |
You could consider an approach using optional chaining syntax, where the receiver object is nullified in production contexts: debug?.assert(...); TypeScript is doing something similar for trace logging. Edit: although thinking about it some more, that might still not allow CFA narrowing as the guard is then only conditionally applied. |
I fail to see how this is relevant. Our checks are meant to be for production, not for local debugging. The part of the check on the left-hand side of the |
To clarify this was closed as "completed" but the limitation has not changed in newer versions. It would be great to reevaluate the possibility to do this given the recent improvements in type narrowing. |
Bug Report
π Search Terms
Control-flow analysis logical
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
It seems that the branches of logical expressions do not impact control flow analysis, and thus the logical expression form is not equivalent to the conditional form, resulting in the type of
b
not being narrowed.π Expected behavior
b
narrowed to thestring
type.The text was updated successfully, but these errors were encountered: