microsoft / microsoft/TypeScript
Inverted union type narrowing was broken in 4.9
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"union type", "narrowing", "inverted", "4.9"
🕗 Version & Regression Information
- This changed between versions 4.8.4 and 4.9.5 (probably caused by https://github.com/microsoft/TypeScript/pull/50397?)
⏯ Playground Link
💻 Code
interface Foo { foo: string; }
interface Bar { bar: string; }
interface Baz { baz: string; }
function myFunction(input: Foo | Bar | Baz) {
const isBaz = 'baz' in input;
const isBar =
//'bar' in input; // This works to narrow `bar` when true and narrow `foo` in the else block
!('foo' in input) && !isBaz; // This works to narrow `bar` when true but does not narrow `foo` in the else block
let x: string;
if (isBaz) {
x = input.baz;
} else if (isBar) {
x = input.bar;
} else {
// In 4.8.4 and earlier, `input` is narrowed to `Foo`
// In 4.9.5 and later, `input` is narrowed to `Foo | ((Foo | Bar) & Record<"baz", unknown>)`
x = input.foo;
}
}
🙁 Actual behavior
In 4.9.5 and later, in the else block, input.foo incorrectly narrows and errors
🙂 Expected behavior
In 4.8.4 and earlier, in the else block, input.foo correctly narrows
Additional information about the issue
No response
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem bereitgestellten Playground-Link und reproduziere den Unterschied zwischen TypeScript 4.8.4 und 4.9.5 anhand des Beispiels im Issue. Verfolge im Type Checker die Typ-Eingrenzung für die invertierten Union-Prüfungen. Als erledigt gilt die Aufgabe, wenn der else-Zweig input auf Foo eingrenzt und input.foo in den betroffenen Versionen ohne Fehler akzeptiert.
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
- 35/100