microsoft / microsoft/TypeScript

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

Offen
#37,251 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: check: Control Flow
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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:

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der verknüpften Playground-Reproduktion unter Verwendung von TypeScript 3.8.3 und vergleiche das Narrowing-Ergebnis mit dem Verhalten von JavaScript bei loser Gleichheit für 0 == "". Überprüfe das Verhalten des Equality-Narrowing und verwandte Randfälle; abgeschlossen ist die Aufgabe, wenn der Checker das Beispiel nicht mehr als sicher eingegrenzt behandelt, sobald die Laufzeitkoerzierung den Vergleich wahr machen kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
32/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.