Skip to content

Discriminated unions and object literals #18629

Closed
@thorn0

Description

@thorn0

I'm not sure if it's a bug.

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

playground, 2.5.1-insiders.20170825

Code

interface Square {
    kind: "square";
    size: number;
}

interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}
type Shape = Square | Rectangle;

var a: Square = { kind: 'square', size: 100, width: 50 }; // error
var b: Shape = { kind: 'square', size: 100, width: 50 };  // oops

Expected behavior:

Same error. From the kind property, TypeScript should understand that the second object literal is of type Square and prevent us from attaching the width property to it.

Actual behavior:

no error

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