Skip to content

Hex Colors error with TS2590: Expression produces a union type that is too complex to represent #59015

Description

🔎 Search Terms

ts(2590), Expression produces a union type that is too complex to represent., HexTypes

🕗 Version & Regression Information

This is an error that typescript throws when I try to build it, as well as the LSP complains. It raises the error ts(2590), Expression produces a union type that is too complex to represent

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAEhAeBhAFgQwE5QLxQEQAZcoAfPARiNNwCZK8BmO3AFiYFYmA2JgdiYA4mATiYBBJgCEmiJgBEmAUSYAxXAG4AUKEiwE9RAHsANgcw4ABgGIAJAG84SNOgC+dhygyv7CDy-ObtaAdOQxMzKCs3Hycvdxiox08E31jopO9EvwDwIIR+UNNsCJsMlOT40oq49OqXcpq0usrPfw0NBDBTYChA3SRjQpwHfQHMUmCCsb780bUgA

💻 Code

type HexChar = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "A" | "B" | "C" | "D" | "E" | "F";
type Hex3Color = `#${HexChar}${HexChar}${HexChar}`;
type Hex6Color = `#${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}`;
type Hex8Color = `#${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}${HexChar}`;

export type HexColor = Hex3Color | Hex6Color | Hex8Color;

🙁 Actual behavior

The type of HexColor should be well defined and accessible. As well as there shouldn't be any errors.

🙂 Expected behavior

The type of HexColor is any, and there are errors.

Additional information about the issue

It seems that also Deno is affected (I first discovered it using Deno). I tried switching to plain TS, still raises errors.

Activity

  1. matveitolochko commented on Jun 25, 2024

    @matveitolochko
    Author

    It seems that the maximum states that union can represent is 2^32, but Hex6Color and Hex8Color are more than that. The simple fix would be to change the max number of union states from a 32-bit integer to a 64-bit integer. I am just not quite sure where that error actually gets raised

  2. MartinJohns commented on Jun 25, 2024

    @MartinJohns
    Contributor

    Duplicate of #43388.

    It seems that the maximum states that union can represent is 2^32

    The limit is 25, and that's intentional.

  3. matveitolochko commented on Jun 25, 2024

    @matveitolochko
    Author

    It seems that the maximum states that union can represent is 2^32

    The limit is 25, and that's intentional.

    I searched for 25, and can't seem to find where this limitation actually is. Is it possible to submit a PR to increase that value ?

  4. jcalz commented on Jun 25, 2024

    @jcalz
    Contributor

    25 is from #30779 but that doesn't seem to be related to this. The limit here is on the order of 10^5, and if you are trying to write a union bigger than that then you're doing something TS doesn't want you to do. The limit is there for performance so I'm sure they're not going to increase it to some large number just because the number can be represented easily.

  5. RyanCavanaugh commented on Jun 26, 2024

    @RyanCavanaugh
    Member

    The limit is in place to stop your typechecking from taking six hours, not because we don't have access to 31 bits worth of integer 😉

  6. matveitolochko commented on Jun 26, 2024

    @matveitolochko
    Author

    I see, thank you. In my case the only thing I can really do then is to perform some analysis during compile time, and/or hope for #43335 to be accepted

  7. typescript-bot commented on Jun 29, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  8. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    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