-
Notifications
You must be signed in to change notification settings - Fork 12.8k
x != y marked as always true when it is not #36534
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 intended as per the TS spec; You can easily opt into the behavior you expect by casting either side of the comparison to if (x as any != y) { } |
That spec doesn't gel with the current behavior, at least for some of the later operators ( To the original point, at the very least the error message is misleading. |
This error message is correct the other 99.5% of the time and it'd be confusing to tack on a "... unless there's an implicit coercion which you shouldn't be doing anyway" clause to the message just to make it Technically Correct |
I feel like "it will always be That said this is probably a duplicate of #26592 |
FWIW trying out TypeScript in a new project I understood the existing message to mean that TypeScript didn't know how JavaScript evaluates. |
Search Terms:
!=, implicit conversion
Code
Expected behavior:
The code should compile successfully. Since != uses implicit conversion it is possible for 'x != y' to be false even if
x
is a number andy
is a string. For example0 != '0'
isfalse
).Note that when the javascript file generated by tsc in the above code is run, the else block is hit as expected.
Actual behavior:
The code has the compilation error:
Playground Link:
http://www.typescriptlang.org/play/?ts=3.8.0-dev.20200128#code/FAYw9gdgzgLgBADzgXjgBgNykrOBPFOAcjSK2AEsAzOACiQEJU8BKOAb2DjnGjABsApgDp+YAOa0iASQhxqcAEZiQAayIssAXziD+UQRy48cAkWMky5eg0pXrNwLcCA
Related Issues:
No
The text was updated successfully, but these errors were encountered: