Skip to content

Lack of expected compiler error with type declaration involving a type union #54784

Closed as not planned
@BrianYoung27

Description

@BrianYoung27

Bug Report

With types AC = {a: string, c: string} and B = {b: string}, setting a variable to {b: 'value of b', c: 'value of c'} with type declaration of AC fails and with B fails but AC|B succeeds.

Maybe I'm missing something here but if the value fails to satisfy AC and B shouldn't it fail to satisfy AC|B and result in a compiler error? Is the type union stripping out the Excess Property Checks for Object literals and letting it satisfy B?

Thanks!

🔎 Search Terms

Type union

🕗 Version & Regression Information

Just noticed it today and repro'd with 5.1.3, Nightly, and all the way back to 3.x in Playground (which probably means this is either known or working as designed). =/

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "union" and "declaration"

⏯ Playground Link

Playground link with relevant code

💻 Code

type AC = {
    a: string, 
    c: string
};
type B = {
    b: string
};

const ShouldAndDoesFail: AC = {
        b: 'value for b',
        c: 'value for c'
};
const ShouldAndDoesFailToo: B = {
        b: 'value for b',
        c: 'value for c'
};
const ShouldFailButWorks: AC|B = {
        b: 'value for b',
        c: 'value for c'
};

🙁 Actual behavior

Setting ShouldAndDoesFail and ShouldAndDoesFailToo results in compiler errors but ShouldFailButWorks does not.

🙂 Expected behavior

Expected ShouldFailButWorks to have a compiler error based on it being declared as AC|B and it fails when declared as either AC or B.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions