Microsoft/TypeScript
GitHub ã§èŠãInterface type coercion fails with unsatisfied properties in unions
Open
#59,716 opened on 2024幎8æ22æ¥
BugDomain: check: Type InferenceHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð 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' })