-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Hex String - Expression produces a union type that is too complex to represent. #43388
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
Comments
Another example, just tried: A 16-digit credit card number: type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
export type CardNumber = `${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}${Digit}`; Interestingly, if I shorten it to this: export type CardNumber = `${Digit}${Digit}${Digit}${Digit}`; It does not complain. |
I wouldn't say it is a duplicate of the first two. They both use more complex type structures. I still think there is a legitimate question in why it is not possible to reduce such a repetitive type to something that can be properly represented. |
That's probably why the TypeScript team didn't close #43335, which definitely is a duplicate of this. Right now it's not working because the feature was simply never intended for something like this, that's why it builds up on union types which have the hard limit. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
I recently ran into this type error. I understand that there are cases where reducing the complexity of a union can be challenging, however in this particular case...I do wonder if it actually is really too complex, given the repetitive nature of the type.
Using the new TS v4 Template Literal types, I have tried to create a type for a MongoDB ObjectId. I started out quite basic:
This spit back the
too complex to represent
error. I then wondered if I transitioned some of the complexity from the template literal type to the union, if that might make a difference:Sadly, this did not help. While TypeScript did not complain about the
Hex
union, it still complained about the template literal. The highly repetitive nature of the type makes me think it should be possible to reduce the complexity to something that actually could be represented.🔎 Search Terms
"Expression produces a union type that is too complex to represent."
🕗 Version & Regression Information
⏯ Playground Link
ObjectId Playground
💻 Code
🙁 Actual behavior
TypeScript was unable to represent the
ObjectId
type due to a union that was too complex.🙂 Expected behavior
TypeScript should be able to handle initially complex unions if the parts of that union are very repetitive... (?)
The text was updated successfully, but these errors were encountered: