Skip to content

Typescript union type with singleton types does not compile #29852

@bomgar

Description

@bomgar

Search Terms:
I did ask here https://stackoverflow.com/questions/54593103/typescript-union-type-with-singleton-types-does-not-compile

Code

type X = {
  id: "primary"
  inverted?: boolean
} | {
  id: "secondary"
  inverted?: boolean
} | {
  id: "tertiary"
  inverted?: false
}


const a = true
const x: X = {
  id: a ? "primary" : "secondary"
}

Expected behavior:
Compiles

Actual behavior:

» node_modules/typescript/bin/tsc test.ts
test.ts:14:7 - error TS2322: Type '{ id: "primary" | "secondary"; }' is not assignable to type 'X'.
  Type '{ id: "primary" | "secondary"; }' is not assignable to type '{ id: "tertiary"; inverted?: false; }'.
    Types of property 'id' are incompatible.
      Type '"primary" | "secondary"' is not assignable to type '"tertiary"'.
        Type '"primary"' is not assignable to type '"tertiary"'.

14 const x: X = {
         ~

Playground Link:
https://www.typescriptlang.org/play/#src=type%20X%20%3D%20%7B%0D%0A%20%20id%3A%20%22primary%22%0D%0A%20%20inverted%3F%3A%20boolean%0D%0A%7D%20%7C%20%7B%0D%0A%20%20id%3A%20%22secondary%22%0D%0A%20%20inverted%3F%3A%20boolean%0D%0A%7D%20%7C%20%7B%0D%0A%20%20id%3A%20%22tertiary%22%0D%0A%20%20inverted%3F%3A%20false%0D%0A%7D%0D%0A%0D%0A%0D%0Aconst%20a%20%3D%20true%0D%0Aconst%20x%3A%20X%20%3D%20%7B%0D%0A%20%20id%3A%20a%20%3F%20%22primary%22%20%3A%20%22secondary%22%0D%0A%7D

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions