microsoft / microsoft/TypeScript
Interface type coercion fails with unsatisfied properties in unions
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
interface type coercion
type coercion
unsatisfied union constraints
property missing in but required
conditional interference
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type coercion
⏯ Playground Link
💻 Code
interface A {
step: 1
}
interface B {
step: 2
}
interface B_Ext extends B {
step: 2
extras: "very cool"
}
type Step = 1 | 2
type Consumer = A | (B | B_Ext);
const func = (): Step => Math.random() > 0.5 ? 2 : 1;
const consumer = (whatever: Consumer) => { }
const step = func();
// ✅ works
if(step === 1) {
consumer({ step })
} else {
consumer({ step })
}
// ❌ fails
consumer({ step })
🙁 Actual behavior
The compiler expects that all properties of a sub-constituent be satisfied
🙂 Expected behavior
The union defines that it is either A | B and B might be B or B & Extras
Additional information about the issue
The same happens with types instead of interfaces as well as a the extras being passed as union of the type:
// causes the same issues
type A = { step: 1 }
type B = { step: 2 } & ({} | { extras: 'very good' })
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 eseguendo la riproduzione collegata di TypeScript Playground e confronta le due chiamate a branch accettate con la chiamata autonoma rifiutata. Traccia il comportamento del compilatore per quanto riguarda l'assegnabilità delle union e la coercizione di interfacce/tipi; il lavoro è completato quando la chiamata autonoma consumer({ step }) viene accettata senza richiedere elementi aggiuntivi, mentre gli esempi esistenti rimangono validi.
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
- 38/100