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 of different types with ==, compiler says it will always evaluate to false #37381

Closed
lhk opened this issue Mar 13, 2020 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@lhk
Copy link

lhk commented Mar 13, 2020

TypeScript Version: 3.8.3

Search Terms:

  • comparison
  • comparisons
  • ==

Code

let a = "1";
let b = 1;

console.log(a == b);

Expected behavior:
Should complain about incompatible types, maybe with a warning that the == operator will lead to an implicit type conversion.

Actual behavior:
It does complain, but the error message is wrong.

error TS2367: This condition will always return 'false' since the types 'string' and 'number' have no overlap.

This is wrong. If the code is executed, the comparison will evaluate to true.

Related Issues:
#30655. In that issue the expected behaviour was that loose comparisons of strings and numbers are considered valid. The issue was closed with a comment that an error message is intended here. I do agree with an error being the intended output. It's simply that the error message is wrong.

#29155 has exactly this problem as title but seems unrelated.

Playground Link: Provided

@WhileTrueEndWhile
Copy link

This should be correct. If you do not use any compiled code will behave right, if it compiles :-). You should not be able to compare different types. So, implicit conversions should not be valid typescript.

@lhk
Copy link
Author

lhk commented Mar 13, 2020

Yes, implicit conversions should not be valid typescript. I do agree.

But the error message is wrong. The condition will not always return false, just look at the playground example (where it returns true).

@nmain
Copy link

nmain commented Mar 13, 2020

Closely related to #37251

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

Exact duplicate of #26592

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

4 participants