microsoft / microsoft/TypeScript

Interface type coercion fails with unsatisfied properties in unions

Aperta
#59,716 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference Help Wanted
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

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgILIN4Chm+QZ0gAcAuZARiwF8stRJZEUAhTHPQiU5AJmtvrR4SZMwD6AUQAeYZBBkQQAE3yi2eAsTJ8N8sFDj4yAIgBu0AJ7IEAexsAbY-yxgLRFAGViyALwVkAD68Lm4oAMI2IPgArgC20L5ogcgAFKxB4tJgAJQA3LS2UbIw0SAIiSnZZF5cvgB8yACycGAAFgB0Bso2sZXIDQAM7QCsyAD8vMhk5PmFhNaRMfFQFQDurS0Q5lBkEVFx0Nn1mMg0BYuynESJJWWV+VgA9I-IgKDkyKs2UADW+HQwKSuvh8fnIR2wGjmBygKQwmlqVGy1Dk9nwKAheChy1h8OuiOcz2QgBlyZDwYCorBY6A4oGIoA

💻 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

  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 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.