-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Comparability of Enum values #10631
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 working as intended. The compiler performs control flow analysis and knows that |
Oh, thanks for clarifying that. It still feels a bit weird.
Well no, it's like writing |
The thing that triggers the stricter behavior is that the type of the variable is one with a finite set of choices (i.e. a union type or an enum type). For example, the following is also an error let p: 0 | 1 = 0;
console.log(p === 1); // Error, comparing 0 to 1 |
I understand. |
You can write In reality this check actually found a lot of bugs when it was created. People would check for an enum value and Generated code should never be in a bad state due to this since the generator itself can know when it would emit unreachable blocks. And usually you wouldn't typecheck generated code anyway (since it ought to be correct). |
In ideal world there would be a flag to disable this globally, but I've noted my position is in the minority. |
TypeScript Version: 2.0.2 Release Candidate
Code
Expected behavior:
true
false
Actual behavior:
error TS2365: Operator '===' cannot be applied to types 'Place.Left' and 'Place.Right'.
The text was updated successfully, but these errors were encountered: