Skip to content

Simplify longer tuple definitionsย #47874

Closed

Description

@Patersooon

Suggestion

๐Ÿ” Search Terms

Simplify longer tuple definitions.

โœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

โญ Suggestion

๐Ÿ“ƒ Motivating Example

type ArrayWithLengthFive = [string, string, string, number, number];
Simplify
type ArrayWithLengthFive = [string * 3, number * 2];

๐Ÿ’ป Use Cases

type ArrayWithLengthFive = [string, string, string, number, number];
Simplify
type ArrayWithLengthFive = [string * 3, number * 2];

I haven't assessed feasibility, but I'd like to mention it anyway๐Ÿ˜.

Activity

  1. IllusionMH commented on Feb 14, 2022

    @IllusionMH
    Contributor

    What if instead of trying shorter cryptic type that starts with 3 strings and ends with 2 numbers (no idea what API can be described this way, instead of an object), it was tuple with labeled elements so any user will know correct order of some strings and some numbers, not only type and count?

  2. jcalz commented on Feb 14, 2022

    @jcalz
    Contributor

    It's weird that the only use case listed for this is the same as the motivating example. Is there some actual real-world situation where tuples like this are being used that are so long as to be hard to use? I'd expect a motivating example to be like [string * 17, number * 7] or something, and a use case to be... like, some description of where such a monstrosity would actually be used.

    Aaanyway, given that #26223 is closed as fixed you can do something like this yourself without too much trouble and without extra syntax:

    type ArrayWithLengthFive = [...TupLen<3, string>, ...TupLen<2, number>];
    // type ArrayWithLengthFive = [string, string, string, number, number]
    
    type LenTwentyFour = [...TupLen<17, string>, ...TupLen<7, number>];
    // type LenTwentyFour = [string, string, string, string, string, string, string, 
    //    string, string, string, string, string, string, string, 
    //    string, string, string, number, number, number, number, number, number, number];
    ๐Ÿ› โš™
    type TupLen<N extends number, T = any, A extends T[] = []> =
    	N extends A['length'] ? A : TupLen<N, T, [T, ...A]>;

    Playground link

  3. fatcerberus commented on Feb 14, 2022

    @fatcerberus

    [string * 17, number * 7]

    This is terrifying

  4. RyanCavanaugh commented on Feb 14, 2022

    @RyanCavanaugh
    Member

    This doesn't come up often enough that we would "spend" syntax/complexity on it.

  5. RyanCavanaugh commented on Sep 24, 2025

    @RyanCavanaugh
    Member

    Alternative you can use btw

    interface TupleOfLength<T> {
      0: [];
      1: [T];
      2: [T, T];
      3: [T, T, T];
      // Keep going as far as you have to (????)
    }
    
    // S = [string, string, string]
    type S = TupleOfLength<string>[3]

    I would recommend this over clever conditional type tricks because it's more performant and there's no reason you can't list out the longest tuple you'd realistically have

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

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions