Skip to content

Constrain consistent discriminating union type usage #44788

Closed
@alexeyMohnatkin

Description

@alexeyMohnatkin

Bug Report

When I use discriminating unions I expect that only one of them will be allowed by TS, not their intersection

🔎 Search Terms

Discriminating union

🕗 Version & Regression Information

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

⏯ Playground Link

Playground link with relevant code

💻 Code

type Common = {foo: string};
type A = Common & {a: string};
type B = Common & {b: string};
type Params = Common | A | B;

// expect error here
const bar: Params = {foo: '123', a: '123', b: '123'};

const myFn = (params: Params) => {}

// expect error here
myFn({foo: '123', a: '123', b: '123'})

🙁 Actual behavior

TS allows using both fields from A and B types

🙂 Expected behavior

TS should treat this as an error

Also if I try to set never for fields that I don't want to be set, it doesn't work

type A = Common & {a: string, b: never};
type B = Common & {a: never, b: string};

There's a workaround for this, but it looks hacky

type A = Common & {a: string, b?: never};
type B = Common & {a: never, b: string};

Activity

MartinJohns

MartinJohns commented on Jun 28, 2021

@MartinJohns
Contributor

Duplicate of #20863. And you also want #12936.

Additional properties is simply something you have to expect and deal with if you want to write robust code, given the current design of the language.

alexeyMohnatkin

alexeyMohnatkin commented on Jun 28, 2021

@alexeyMohnatkin
Author

@MartinJohns thank you.

For those who are looking for an answer:

type Common = {foo: string};
type A = {a: string, b?: never};
type B = {a?: never, b: string};
type C = {a?: never, b?: never};
type Params = Common & ( A | B | C);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @MartinJohns@alexeyMohnatkin

        Issue actions

          Constrain consistent discriminating union type usage · Issue #44788 · microsoft/TypeScript