Skip to content

How to use just one of interfaces with different properties #28879

@sorryformystupidquestions

Description

TypeScript Version: 3.3.0-dev.20181206

Search Terms: interface, union

Code

type WithId = {
  id: number
}

type WithNumber = {
  number: number
}

type User = WithId | WithNumber

const user: User = {
    id: 1,
    number: 1
};

Expected behavior: user cannot have both id and number properties

Actual behavior: user can have both id and number properties. How can I achieve this?

Playground Link: http://www.typescriptlang.org/play/#src=type%20WithId%20%3D%20%7B%0D%0A%20%20%20%20id%3A%20number%0D%0A%7D%0D%0A%0D%0Atype%20WithNumber%20%3D%20%7B%0D%0A%20%20%20%20number%3A%20number%0D%0A%7D%0D%0A%0D%0Atype%20User%20%3D%20WithId%20%7C%20WithNumber%0D%0A%20%20%0D%0Aconst%20user%3A%20User%20%3D%20%7B%0D%0A%20%20%20%20id%3A%201%2C%0D%0A%20%20%20%20number%3A%201%0D%0A%7D%3B

Related Issues: #12745, #20060, #23535

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