-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Comparison operator error message misleading #40496
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
My personal preference would be to resolve the original issue and prevent comparisons across types, which would make this issue moot. But the motivation described in that issue to allow such code patterns seems reasonably decided. For motivation consider a following mistake: const numberOfUsers: "loading" | number;
// Seemingly correct code outputs an incorrect message.
if (numberOfUsers > 5) console.log('You’ve got a big team!')
else console.log('You’ve got a small team. :-(') |
See #26592 |
#26592 seems very related, but I’m not sure if it’s a duplicate. That issue talks about getting a “will always return 'false'” when not expected, whilst I require the opposite. It still makes sense if you want to group them there, I just double check that you’ve not misread my issue description. |
I think if we're doing something with these messages, we should address them as a group, yes. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Comparing a string to a number generates a TypeScript error saying we can not apply a comparison operator to strings with numbers. This is misleading, since if I truly could not apply a comparison operator to strings with numbers, I could not do that even if a string is part of a bigger union. This results in a perceived inconsistency:
The real motivation behind the compiler warning is not the concern about comparing incomparable types, but rather that comparing strings to numbers results in a constant condition. (#13863 (comment)) This resolves the inconsistency between the two similar pieces of code, but it introduces an inconsistency between the error message and the error meaning.
If it was possible to rephrase the error to say “This condition will always return false” or “This condition is constant”, like in case of
if (0 !== "")
, the motivation would be immediately self-evident and not mislead.TypeScript Version: v4.1.0-insiders.20200910
Search Terms: operator can not be applied, number, bigger, less, comparison operator, union.
Code
Expected behavior: An error “This condition will always return false” on line 2.
Actual behavior: An error “Operator '>' can not be applied to types 'number | string' and 'number'” on line 2.
Playground Link
Related Issues: #13683
The text was updated successfully, but these errors were encountered: