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

Simplify longer tuple definitions #47874

Closed
5 tasks done
Patersooon opened this issue Feb 14, 2022 · 4 comments
Closed
5 tasks done

Simplify longer tuple definitions #47874

Patersooon opened this issue Feb 14, 2022 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@Patersooon
Copy link

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

@IllusionMH
Copy link
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?

@jcalz
Copy link
Contributor

jcalz commented Feb 14, 2022

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

@fatcerberus
Copy link

[string * 17, number * 7]

This is terrifying

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Feb 14, 2022
@RyanCavanaugh
Copy link
Member

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

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