Skip to content

Interface type coercion fails with unsatisfied properties in unions #59716

Description

@mio-moto

🔎 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

interfaceA{step: 1}interfaceB{step: 2}interfaceB_ExtextendsB{step: 2extras: "very cool"}typeStep=1|2typeConsumer=A|(B|B_Ext);constfunc=(): Step=>Math.random()>0.5 ? 2 : 1;constconsumer=(whatever: Consumer)=>{}conststep=func();// ✅ worksif(step===1){consumer({ step })}else{consumer({ step })}// ❌ failsconsumer({ 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 issuestypeA={step: 1}typeB={step: 2}&({}|{extras: 'very good'})

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions