-
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
Unnormalized template literal types #40731
Comments
It seems like your use cases are already covered by existing type system features, which is usually far preferable to introducing new syntax. Is there anything besides ergonomics that something like this lacks that your suggestion addresses? |
I guess I should add that the proposal is specifically for dealing with assignability. Generics definitely get the job done for isolated strings, but things get hairy if you want to use the validation in an interface declaration, array of validated strings etc. |
How would you propose determining if one |
Firstly, let me adjust the proposed syntax as I think that will make things clearer: type Hex = `${unnormalized 0 | 1 | 2 | 3... "F"}`;
type HexColor = `#${Hex}${Hex}${Hex}${Hex}${Hex}${Hex}`;
type Example = `${unnormalized "A" | "B"} ${"C" | "D"}`;
// expands to `${unnormalized "A" | "B"} C` | `${unnormalized "A" | "B"} D` The idea is that the type behaves in the same way as pattern literal types, but allows for a union of literals in place of a primitive. I would expect the same assignability rules, except that when two The goals are definitely aligned with #6579, though my intent is to suggest a minor syntax addition in order to close the perceived gap in behavior between template literal types and pattern literal types. Say I want to ensure 5 space-separated values in a string: type Placeholder = string;
type MyString = `${Placeholder} ${Placeholder} ${Placeholder} ${Placeholder} ${Placeholder}`; This is valid syntax in TS nightly and it does the validation I asked for. I can't distribute over this type, I can't narrow it through control flow. I can take a union with another type literal, I can't take an intersection. Say I want to limit what any of these five values can be. I change I will look through comments on performance considerations of #6579 because that issue might be enough to shoot this suggestion down. Still, I think it's worth pointing out that the two extant syntaxes discussed have divergent behaviors, one of which results in limitations in service of a feature set that the user might not have wanted anyway. |
I don’t think that’s sufficient, but notably, it appears that pattern literal types simply aren’t assignable to each other unless they’re identical: https://www.typescriptlang.org/play?ts=4.1.0-dev.20200924#code/C4TwDgpgBAKhDOwCMUC8UAGASA3ogTgJYB2A5gL5QCCGAUKJLAsAExqZS4EkXV20ATCAGMANgEN80URGBQIAD3EBbMDIBcTREgDcgkRKlQZcxSrUQWmuIhZ6zqmW3QOL9pY+guPFu7SA
As an aside, intersections seem to work as expected: https://www.typescriptlang.org/play?ts=4.1.0-dev.20200924#code/C4TwDgpgBAKhDOwCMUC8UAGASA3ogTgJYB2A5gL5QCCGAUKJLAsAExqYBCuBJF1dtAMYB7YoigAPAFxNEKAGSzW7AEQdqK2kA |
“Conditional Assignability” discussed in the design meeting today (#40779) would subsume this proposal. |
That's a really neat idea. It's syntactically way clearer than either of my suggestions, and I think TS in general would benefit from having an inference mechanism like that for assignments. Thanks for sharing this! |
Search Terms
template literal types, normalization, string validation
Suggestion
An
unnormalized
keyword that can be applied to template literal types to opt out of normalization. Instead of producing a union representing the cross product of each variable in the template, the original declaration is preserved. The template literal type is partially expanded when compared to another type, so the checker can avoid calculating the complete cross product. For example:For an assignment where the second digit is invalid, this would reduce the number of comparisons by orders of magnitude and spare the memory overhead of calculating the cross product:
I believe a feature of this sort is warranted as the current template literal syntax has a curious disconnect with the "pattern literal" syntax. The
Zip
example above is currently unfeasible because it produces a very large union for a relatively simple constraint. In comparison, something likehas no such problem in spite of having infinitely more permutations.
I'm completely open to nomenclature*, syntax & behaviour suggestions, but I feel strongly that this should be an opt-in behaviour and not implicit in any way, as it represents the user selecting one feature set (string validation) as opposed to another (type unions).
*emphasis here–whichever way this syntax is expressed, it should communicate to the user that the result will not be a type union, which is the typical behaviour. Maybe
validate
would be better.Use Cases
This would be useful for cases where the type is intended for validation only of a string, and the user doesn't require the type to be compatible with features of union types such as mapped types, distributive types, type narrowing. The examples below show a common topic in relevant PRs: validating that a string matches a 24 bit hex value. I believe that users who want to strictly type strings with millions of permutations are unlikely to need to apply features of union types to them... though I would love to see what 16.7 million grouped
if/else
statements look like.Examples
Pain Points
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: