-
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 guard handling with boolean compares #9508
Comments
There are limits that the compiler goes to when trying to statically analyse the code. JavaScript has lots of patterns that are perfectly valid at run-time, but aren't necessarily worth coding for in the compiler for... For example all of these are "valid" statements: Array.isArray(myAny) === !!'0';
Array.isArray(myAny) != undefined;
Array.isArray(myAny) === Boolean(1);
Array.isArray(myAny) == 1;
Boolean(Array.isArray(myAny)) !== false;
Array.isArray(myAny) > false; |
Yes, there are always esoteric difficult usecases, but my usecase was I had to change the code to the less readable |
The more powerful type guards become, the more expectactions there are that they should work for all cases that can be statically reasoned. I think there were less issues raised back when they were rudimentary and people's expectations were much lower! One possible solution would be if TypeScript eventually got compile-time expression evaluation/simplification facilities as exist in some other languages/compilers (eg C++, D). Then any ridiculous expression, like |
|
This doesn't seem common enough to support. In most cases |
TypeScript Version: 1.8.0
Code
Expected behavior:
All three should result in the same type guard.
Tested with Visual Studio 2015 Update 2 and TypeScript Playground mouse hover.
The text was updated successfully, but these errors were encountered: