You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
functionfoo(x: string|number,y: string|boolean){if(x==y){// We can now call any 'string' method on 'x' or 'y'.x.toUpperCase();y.toLowerCase();}else{console.log(x);console.log(y);}}foo(0,"");
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 and string | boolean to string, since a number can equal a string in the edge case 0 == "".
Actual behavior:
After a loose equality comparison, TS narrows down string | number and string | boolean to string, even though this is incorrect, as seen for edge cases like 0 == "".
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: #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.
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:
The text was updated successfully, but these errors were encountered: