-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type narrowing not working as expected in else #26852
Comments
TypeScript narrowing doesn't do arbitrary Boolean algebra; it just looks for conjuncts of specific forms in the path condition of each path to the operation of interest. You could turn this issue into a suggestion, but I'm guessing it will be declined on performance grounds. Or you could just rewrite the code as: function test(base: Base): void {
if (!base.b) {
if (!base.a) {
const result: null = null;
} else {
const result: {} = base.a;
}
} else {
const result: {} = base.b;
}
} |
@mattmccutchen makes sense, it's a suggestion/feature request then. It seems like something that could make type narrowing even more powerful than it is, I'm not sure what the performance challenges are though. |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
@DanielRosenwasser I think this is labelled incorrectly and shouldn't have been closed, it never really got an answer on why or why not from a maintainer. |
Sorry, I don't know why this was labeled as a question. |
For the following code with
strictNullChecks
enabled:I'm seeing this error on the last result:
I expect it to work as the else implies:
TS play link
The text was updated successfully, but these errors were encountered: