Skip to content

Type narrowing in loose equality fails for edge cases like empty string and zero #37251

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

Open
vwkd opened this issue Mar 6, 2020 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@vwkd
Copy link

vwkd commented Mar 6, 2020

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.

function foo(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 == "".

Playground Link: Playground Link

Related Issues:

@nmain
Copy link

nmain commented Mar 6, 2020

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

@getify
Copy link

getify commented Mar 6, 2020

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.

@psmolak
Copy link

psmolak commented Jul 18, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants