Skip to content
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

Comparison operator error message misleading #40496

Closed
denis-sokolov opened this issue Sep 11, 2020 · 5 comments
Closed

Comparison operator error message misleading #40496

denis-sokolov opened this issue Sep 11, 2020 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@denis-sokolov
Copy link

denis-sokolov commented Sep 11, 2020

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:

const s = "s"
if (s > 0) {} // Comparing strings to numbers, TypeScript error “can not do that”

const u = (0 as number | string)
if (u > 0) {} // Comparing strings to numbers sometimes, no TypeScript error

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

const s = "s"
if (s > 0) {}

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

@denis-sokolov
Copy link
Author

denis-sokolov commented Sep 11, 2020

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. :-(')

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 11, 2020
@RyanCavanaugh
Copy link
Member

See #26592

@denis-sokolov
Copy link
Author

#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.

@RyanCavanaugh
Copy link
Member

I think if we're doing something with these messages, we should address them as a group, yes.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants