-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Treat string literals as readonly char arrays while compering them #51217
Comments
This misapprehends the root of the limitation; "treat as char array" is not a fix. The root problem is that if you write something like type X = `${"A" | "B"}`; Then we have two options. We can flatten it: type X = "A" | " B"; or leave it alone in its original form. Leaving it alone in its original form works for a second, but the moment you do something more interesting with the type, the limitation immediately appears again: type F<T> = T extends "A" ? true : false;
type G = F<X>; Here, |
I don't think you understand what I'm trying to say. The goal here is instead of flattening the template and finding every single possible f/cking string literals and doing a check for 100,000,000 something strings, we can just check every char one by one. So hex color example would look like this: type HexChars = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type HexColor = `#${HexChars}${HexChars}${HexChars}` | `#${HexChars}${HexChars}${HexChars}${HexChars}${HexChars}${HexChars}`
const color: HexColor = "#fff" Would work like this under the hood: type HexChars = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type HexColor = ['#', HexChars, HexChars, HexChars] | ['#', HexChars, HexChars, HexChars, HexChars, HexChars, HexChars]
const color: HexColor = "#fff" // ['#', 'f', 'f', 'f'] EDIT: I think the problem here is, that I can do this: type HexChars = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type HexColor = ['#', HexChars, HexChars, HexChars] | ['#', HexChars, HexChars, HexChars, HexChars, HexChars, HexChars]
const color: HexColor = ['#', 'f', 'f', 'f'] But can't do this: type HexChars = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
type HexColor = ['#', HexChars, HexChars, HexChars] | ['#', HexChars, HexChars, HexChars, HexChars, HexChars, HexChars]
const color: HexColor = "#fff" |
That works fine for simple assignability checks, but there are still cases that would require the template to be expanded into a union of all the possible strings (distributive conditional types, e.g.). And changing template strings to not behave as unions in those cases would be a huge breaking change. Thatβs what Ryan was trying to explain. |
Based on your reply, I understood the original comment correctly, and my response to that still applies. It sounds like you're looking for RegExp types, see #41160 |
Cool, having RegExp types would be a lot more useful |
Suggestion
π Search Terms
string literal, template, complex, string literals as array
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I was just experimenting with this feature #40336
As mentioned in the PR:
We can't make the string literals too complex.
If we do we get this error:
Expression produces a union type that is too complex to represent.
But this error is not caused because the type we wanna check is too complex.
It's cause because IDE generates every possible literal string type.
But instead of that we can treat strings as char arrays while compering them.
So no breaking changes, everything same but we compare string literals as if they are readonly char arrays
And after achieving this string unions can become less complex.
π Motivating Example
For example i wanna check if a string is a HEX color
I can write something like this
But this is gonna give us this error:
Expression produces a union type that is too complex to represent.
Because it tries to generate every possible string literal
But TypeScript is quite capable of checking if a string is a hex color.
Example above is able to check if a string is a hex color or not.
So if we were to treat strings as readonly char arrays while compering them, the first example above would have worked without any issue.
π» Use Cases
This can be use for many things.
The text was updated successfully, but these errors were encountered: