Skip to content

Add a new type Tuple<3, T> = [T, T, T]Β #54740

Closed as not planned
Closed as not planned

Description

Suggestion

πŸ” Search Terms

tupel

βœ… 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

Add a Tuple type where we can dynamically create tuples:

type Tuple<L, T> = [T, T, T]; // L = Length of Tuple

πŸ“ƒ Motivating Example

This type would be in the same area as the Awaited type because it helps preventing complex type definitions.

Tupel-types currently have to be created manually each time. Especially for longer fix length arrays, this gets complicated really fast:

type FixLength6Array = [number, number, number, number, number, number] ; 
const fixLength6Array: FixLength6Array = [1,2,3,4,5,6];

By using a Tuple type, we could fix this:

type FixLength6Array = Tuple<6, number>;

Also if we wanted to change the type of this tuple, this is done easily and has to be done in one place:

type FixLength6Array = Tuple<6, number | string>;

πŸ’» Use Cases

What do I want to use this for?

One example would be an rgb-type which allows numbers and strings. It currently looks like this:

type NumberString = string | number;
type RgbTuple = [NumberString, NumberString, NumberString];

We could make this much simpler like this:

type RgbTuple = Tuple<3, NumberString>;

Activity

  1. olivermrose commented on Jun 22, 2023

    @olivermrose
  2. nmain commented on Jun 22, 2023

    @nmain

    #51841 (comment)

    But is the request here just a simple utility type? As far as I know, there's no way you can write Tuple<L, T> in your own code right now. T[] & { length: L } doesn't stop you from indexing outside of the expected range.

  3. fatcerberus commented on Jun 22, 2023

    @fatcerberus

    If the request is more than just the utility type then this is an essentially a duplicate of #47874 with different syntax.

  4. fatcerberus commented on Jun 22, 2023

    @fatcerberus

    As for the utility type itself, apparently you can already implement it today: #47874 (comment)

  5. RyanCavanaugh commented on Jun 22, 2023

    @RyanCavanaugh
    Member

    As far as I know, there's no way you can write Tuple<L, T> in your own code right now.

    type TupleMap<T> = {
      0: [],
      1: [T],
      2: [T, T],
      3: [T, T, T]
      // go as far as you need to
    }
    type TupleN<N extends keyof TupleMap<unknown>, T> = TupleMap<T>[N];
    
    type RGB = TupleN<3, number>;

    I really recommend the lookup type method over tail-recursion wizardry in any case where you have a reasonable finite bound; it's generally more performant and easier to read.

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