-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Template Literal Types with fixed or limited Length #52243
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
Possibly out of scope for this issue (or possibly in scope) it would also be useful to support arbitrary length, so one could have a "hex encoded data" type which allows a certain subset of characters but can be any length. This would be used for dynamic length byte arrays encoded as a hex string. |
Related: #41160 |
With high confidence, the only way we'd address this scenario is through regex types. It wouldn't make sense to have template strings have three categories (finite, big-but-finite, and infinite) since that middle one would be very difficult to reason about as to how it relates in unions |
Would it be reasonable to create an issue for unbounded length like |
You can log it but I don't think it'd happen. The intended use case for template string literals was and still is property name manipulation and extraction, not data format validation of runtime strings. Regexes are much better suited for that. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
Provide a way for a user defined template literal type to be a fixed length or limited length.
π Search Terms
Template Literal Type Length
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
An intrinsic string manipulation type similar to
Uppercase<StringType>
, but that results in a fixed length string with provided substring union repeatedn
times.π Motivating Example
π» Use Cases
A lot of the work I do deals with byte arrays that are passed around as strings (because they are often pulled directly out of a JSON payload). These usually have a well defined length and a consistent prefix. For example:
I would like to be able to write functions that are constrained to accept only strings of a certain length, and ideally only made up of certain characters. Currently, the best I can do is type as
0x${string}
, but this doesn't allow me to differentiate between a 20 byte hex string and a 32 byte hex string which are effectively different types in this context and are incompatible with each other.The text was updated successfully, but these errors were encountered: