-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Logical successive type guards #37258
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
Making the compiler reason about this sort of thing sounds really cool until you realize how slow it would be to implement. Even in this toy baby case, in order to narrow
|
As mentioned this would be very complex, plus there are relatively few type domains that can be exhausted by one or two checks. Structuring this code in a way that's more compiler-friendly will also make it more human-friendly so I don't think it's too bad if this doesn't work. |
@RyanCavanaugh how would you restructure this code to be more compiler/human friendly? It seems pretty straightforward. I don't disagree with closing this ticket due the complexity of the solution, but just curious about how else you'd structure this. 2 new functions for context:
|
For the record, I don't appreciate the issue-closing comment to be along the lines of "write better code". Feels like a cop-out. |
TypeScript Version: 3.9.0-dev.20200306
Search Terms:
successive type guards, conditional type guards, type guards
Code
Expected behavior:
a
andb
cannot logically be null, therefore it should be inferred that these are strings.Actual behavior:
It is inferred that these variables are possibly null.
Playground Link:
https://www.typescriptlang.org/play/?ts=3.9.0-dev.20200305&ssl=1&ssc=1&pln=21&pc=2#code/GYVwdgxgLglg9mABDAzgORAG0wCgIYBciKUATjGAOaIA+iYWmAlEXsivY4gN4BQiiUgFMoIUkjYBeSZ2wBuXgF9evUJFgJEAEzgBlUcGD4iJclVqzMAGkQAjE2QrU6DbEx79kwRPkTTLiABkgXZ+Mq7MHgICwqLiCgLKnjDevv4RQSG2iACE4YzufNGCImJgCYhJAik+bHkBwaHpBVHRsWUVVYh4cogA9H2ImHCUMBB42ACeiAC2ICR2QogABqZOyza2IFDIYMBCpMJa3RyrjuYujMuetr0DQyNjE5jTcwu2S2dmlBt227v7Q5CY54U5rC6Wa4CPAAOkwQioUAAFndBgB5WwAKyE0HYiAADnAUCgYLYXogAOQRClKIA
Related Issues:
#9016
The text was updated successfully, but these errors were encountered: