microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.89k stars 12.47k forks source link

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

Open vwkd opened 4 years ago

vwkd commented 4 years ago

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 commented 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

getify commented 4 years ago

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 commented 1 year ago

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