-
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
Normalize Specific String Length Behavior with Tuple's #43134
Comments
I thought that the normalization of the indices of specific string types and tuples should not be included in this as it seemed too broad. However, if anyone would like to write out an issue for that that would normalize the type behaviors and allow for the following code to work as indicated then please do so: type Name = "Carter";
type FirstLetter = Name[0]; // "C"
type AllLetters = Name[number]; // "C" | "a" | "r" | "t" | "e" | "r"
type OutOfBoundsLetter = Name[6]; // error: String type 'Name' has no character at index '6'. [Very similar to tuple out of bounds error] |
That sounds like a poor use-case for this, it won't prevent invalid strings to be passed. #41160 would work for this. |
Sorry, I was trying to explain one of the use-cases of being able to cap strings at a specific length. A use case that can be solved by having the type CryptoPublicKey = string & { length: 64 };
type CryptoSecretKey = string & { length: 128 };
const public: CryptoPublicKey = "b2adab9840a26690b15c24bd7f1b19d317468d06e21e84f6333350263bc62c5c";
const secret: CryptoSecretKey = "ea01f13a513086dffeeb13d871e8c4fc38a6b19e8d203969c650e992a84139a374505c0e5ac292e0bafab13021c8fa76c63bf4be1759b324b95a9c9c0f2be0da";
const badSecret: CryptoSecretKey = "ea0"; // error: type 'ea0' is not assignable to type 'string & { length: 128 }'. '3' and '128' are not compatible. You would basically have the power to not only get the |
Would a string containing only emojis be a valid This just seems like the wrong approach. None of your suggestions would be solved by having a specific length on the string. |
I absolutely know we have an issue on this already, but cannot find it π’ |
I thought so as well, but couldn't find it either... until now: #34692 Turns out we both commented on it. |
Closing in favor of #34692 |
Thank you for saving my sanity @MartinJohns π |
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Currently, tuple types have the
length
property intrinsicly computed as shown in the folllowing:However, for specific string types such as
"Joe"
, thelength
property is still of typenumber
:I would like to propose that the
length
property be computed in the type type system such thatPerson["length"]
(as shown in the previous example) would evaluate to3
(the proper length of the string) instead ofnumber
.Now, time for a few of the hairy details.
One of them is how the behavior for the property would be when the type is a union of strings. The best way to define this behavior is by following what happens when you get the
length
property on a union of as defined in the following code:And the string behavior for getting the length of union strings should not contain any "magic" either, just give out a union of each of the string's length:
The generic
string
type should also behave exactly like a genericArray
type when you are getting thelength
.With union types for the strings when combining
string
and a specific string type such as"hello"
, thestring
length
should always remain supreme. This behavior is not exactly unexpected because unions likestring | "foo" | "asdf"
evalute to juststring
.π Motivating Example
The feature would improve TypeScript because it makes the behavior of similarly-structured concepts (specific string and tuple) more universal. Currently, if I wanted to get the length of a string type, I would have to do something like the following:
However, the
Chars
type will easily overload due to its recursive nature:π» Use Cases
The ability to get the length of a string type just by accessing the
string
property has many benefits. One of which is being able to lock a string to a specific length for something such as a date in which you want to specify the number of characters that come in each part.The text was updated successfully, but these errors were encountered: