Skip to content
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

Closed
pocesar opened this issue Apr 22, 2018 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@pocesar
Copy link

pocesar commented Apr 22, 2018

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

type LimitedString = string(3);

let limitedString: LimitedString = '1234' // error, LimitedString expects at most 3 characters
limitedString = '12' // would work (or not?)

type LimitedArray = Array(3)<{ something: string }>

let limitedArray: LimitedArray = [{ something: '' }, { something: '' }, { something: '' }, { something: '' }] // error, LimitedArray expects at most 3 members

type LookupStates = {
  [index: string(2)]: string
}

// This is useful when you have a normalized array or lookup table, for example
const states: LookupStates = {
  'KS': 'Kansas',
  'IA': 'Iowa'
}

states['IOWA'] // error
@j-oliveras
Copy link
Contributor

Array case is already supported:

type LimitedArray = [{ something: string }, { something: string }, { something: string }];

let limitedArray: LimitedArray = [{ something: '' }, { something: '' }, { something: '' }, { something: '' }]

limitedArray has an error:

Type '[{ something: string; }, { something: string; }, { something: string; }, { something: string; }]' is not assignable to type '[{ something: string; }, { something: string; }, { something: string; }]'.
  Types of property 'length' are incompatible.
    Type '4' is not assignable to type '3'.

Is not the same the type string[] and [string] (the first is an array without limited length, the second is and array with only one string item).

For strings, exists a pull request by @weswigham experimenting with regex validated strings types: #21044.

@pocesar
Copy link
Author

pocesar commented Apr 22, 2018

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...

@mhegazy
Copy link
Contributor

mhegazy commented Apr 23, 2018

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 Array. e.g.

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.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 23, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants