Skip to content

Compiler loses track of nested union types #21165

@thw0rted

Description

@thw0rted

TypeScript Version: 2.6.2

Code

type ItemOptions = { id?: string; };
type Item = ItemOptions & { id: string; };
type ContainerOptions = { items: ItemOptions | ItemOptions[]; };
type Container = ContainerOptions & { items: Item | Item[]; };

let c1: ContainerOptions = {
    items: {
        id: "1",
        x: 2   // Good, error
    }
}

let i1: (Item | Item[]) & (ItemOptions | ItemOptions[]) = {
    id: "1",
    x: 2   // Good, error
};

let c2: Container = {
    items: {
        id: "1",
        x: 2   // Bad, no error!
    },
};

Expected behavior:
Compiler should notice the extraneous x object property in all 3 cases.

Actual behavior:
Compiler correctly flags "Object literal may only specify known properties" for the first 2 instances (ContainerOptions and Item union/intersection) but not the third (Container).

Related:
I'm kind of new at this whole type union/intersection thing, so it's entirely possible that the type declarations above aren't sane in the first place. If that's the case, I'd appreciate any feedback on this closely related SO question.

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