Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AtomicGamer9523 opened this issue Jun 25, 2024 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@AtomicGamer9523
Copy link

🔎 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.

@AtomicGamer9523
Copy link
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

@MartinJohns
Copy link
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.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 25, 2024
@AtomicGamer9523
Copy link
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 ?

@jcalz
Copy link
Contributor

jcalz commented Jun 25, 2024

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.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jun 26, 2024

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 😉

@AtomicGamer9523
Copy link
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

@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants