Skip to content

Feature Request: strictier array and string using parenthesis modifiers #23617

Description

@pocesar

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

Activity

  1. j-oliveras commented on Apr 22, 2018

    @j-oliveras
    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 Wesley Wigham (@weswigham) experimenting with regex validated strings types: #21044.

  2. pocesar commented on Apr 22, 2018

    @pocesar
    Author

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

  3. mhegazy commented on Apr 23, 2018

    @mhegazy
    Contributor

    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 Jordi Oliveras Rovira (@j-oliveras).

  4. typescript-bot commented on May 7, 2018

    @typescript-bot
    Contributor

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

  5. locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions