-
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
Feature Request: strictier array and string using parenthesis modifiers #23617
Comments
Array case is already supported: type LimitedArray = [{ something: string }, { something: string }, { something: string }];
let limitedArray: LimitedArray = [{ something: '' }, { something: '' }, { something: '' }, { something: '' }]
Is not the same the type For strings, exists a pull request by @weswigham experimenting with regex validated strings types: #21044. |
the only problem with both approaches is that either the regex guard than the tuple type have problems with documentation (and plus the verbosity). what if the array is 30 items long? how to show the "quickinfo" in an IDE for the regex guard type? you won't know the format until you read the typings file... |
Do not think we should be mixing strings and arrays, there is really no reason why these scenarios should be combined. Arrays with fixed length are handled to an extend with tuples since they now have fixed length (see #17765). You can already today have a tuple-like type using interface FixedLengthArray<T, L extends number> extends Array<T> {
"0": T;
length: L;
}
let limitedArray: FixedLengthArray<E, 3> = [{ something: '' }, { something: '' }, { something: '' }, { something: '' }] // error, 4 is not assignable to 3 Strings should be handled with regex validated strings types: #21044 as noted by @j-oliveras. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.7.0-dev.201xxxxx
Search Terms: string, array, length, modifier, strict
Some strictly typed languages can catch string / array overflows on compile time against hard coded values, and might help catching some bugs early on. Usually when creating public APIs, sooner or later, you have to implement a max length for either strings or array items, and while you can catch the error AFTER you've made the request, you would have been able to avoid it at the first place.
This helps with validation libraries as well, such as Joi, that can create highly dynamic object schemas.
Code
The text was updated successfully, but these errors were encountered: