microsoft / microsoft/TypeScript

narrowing doesn't work when spreading from ternary operator

Aperta
#45,769 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

narrow spread ternary

🕗 Version & Regression Information

v4.5.0-dev.20210907

⏯ Playground Link

Playground link with relevant code

💻 Code
interface Foo {
  type: "foo"
  foo: string
}

interface Bar {
  type: "bar"
  bar: number
}

declare const thing: Foo|Bar

const otherThing: Foo|Bar = { //error: Type '"foo" | "bar"' is not assignable to type '"foo"'.
  type: thing.type,
  ...(thing.type === "foo" ? { foo: thing.foo } : { bar: thing.bar })
}
🙁 Actual behavior
error: Type '"foo" | "bar"' is not assignable to type '"foo"'.
🙂 Expected behavior

no error, since it's not possible for the object to not match either the Foo or Bar types

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e l'esempio minimo Foo|Bar. Traccia come il type checker gestisce il narrowing all'interno di un'espressione ternaria usata in un object spread. Il lavoro è completato quando l'esempio supera il controllo dei tipi senza errori, preservando al contempo il controllo corretto dei rami di spread incompatibili.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.