microsoft / microsoft/TypeScript
Inverted union type narrowing was broken in 4.9
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il link al Playground fornito e riproduci la differenza tra TypeScript 4.8.4 e 4.9.5 usando l’esempio nell’issue. Traccia il restringimento del tipo per i controlli invertiti dell’unione nel type checker. Il lavoro è completato quando il ramo else restringe input a Foo e accetta input.foo senza errori nelle versioni interessate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100