microsoft / microsoft/TypeScript
Interface type coercion fails with unsatisfied properties in unions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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' })
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked TypeScript Playground reproduction and compare the two accepted branch calls with the rejected standalone call. Trace the compiler's union assignability and interface/type coercion behavior; done means the standalone consumer({ step }) call is accepted without requiring extras, while the existing examples remain valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100