Skip to content

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

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

Closed
5 tasks done
chwoerz opened this issue Jun 22, 2023 · 5 comments
Closed
5 tasks done

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

chwoerz opened this issue Jun 22, 2023 · 5 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@chwoerz
Copy link

chwoerz commented Jun 22, 2023

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>;
@xiBread
Copy link

xiBread commented Jun 22, 2023

#51841 (comment)

@nmain
Copy link

nmain commented Jun 22, 2023

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

@fatcerberus
Copy link

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

@fatcerberus
Copy link

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

@RyanCavanaugh
Copy link
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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Jun 22, 2023
@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants