Open vwkd opened 4 years ago
I think this is a duplicate, but I couldn't find anything exact; hard to search for loose equality / double equals / sloppy equals. There is some related discussion here: https://github.com/microsoft/TypeScript/issues/30540
I dunno whether it's a duplicate, but it definitely is a problem that TS is making incorrect assumptions/assertions about how ==
will work. I read some of the other threads, and the response tone seems to be "who cares?", but I think this is a problem that should be addressed.
In this cited case, I don't think there's any valid type narrowing that should apply, so if it is being applied, TS should fix that bug.
I also found this problem recently while going through TS Handbook. My case is the following:
function compare(first: string | number, second: string | boolean) {
if (first == second) {
console.log(typeof first);
console.log(typeof second);
}
}
compare(10, "10");
The narrowed types inside the if
statement reported by TS are string
for both first
and second
arguments when in reality they are number
and string
.
Playground link
TypeScript Version: 3.8.3
Search Terms: type narrowing loose equality type guard loose equality loose comparison empty string zero non-strict comparison empty string zero type guard empty string zero coercion double equals type guard coercion fails
Code
TS misses edge cases when narrowing down types in loose equality comparisons.
Following the example from the new handbook on Equality narrowing, if you replace the strict equality with a loose equality, you're rocket is ready to explode. If you pass in the empty string and zero the if condition passes and accessing
toUpperCase()
on a number fails.It might be worth mentioning, that this is only one out of many edge cases with the loose equality operator. Maybe you want to check more than just my example above. I bet Kyle Simpson (@getify) could give you some more examples.
Expected behavior:
After a loose equality comparison, TS should not narrow down
string | number
andstring | boolean
tostring
, since a number can equal a string in the edge case0 == ""
.Actual behavior:
After a loose equality comparison, TS narrows down
string | number
andstring | boolean
tostring
, even though this is incorrect, as seen for edge cases like0 == ""
.Playground Link: Playground Link
Related Issues: