Repository navigation
Feature Request: a rest parameter of type "Array of Tuples" should expand to a repeating parameter list of the Tuple's type #56776
Copy link
Copy link
Closed
Description
🔍 Search Terms
Tuple, varargs, rest parameter, rest argument, function signature, variadic tuple types, spread syntax
✅ Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Here is a playground demonstrating the functionality I expected from the function signatures:
declare const tupleRestParams: (
input: string,
...translations: [string, string]
)=> string;
tupleRestParams("input", "a"); // errors as expected
tupleRestParams("input", "a", "b"); // compiles as expected
tupleRestParams("input", "a", "b", "c"); // errors as expected
tupleRestParams("input", "a", "b", "c", "d"); // errors as expected
tupleRestParams("input", "a", "b", "c", "d", "e", "f"); // errors as expected
declare const tupleArrayRestParams: (
input: string,
...translations: [string, string][]
)=> string;
tupleArrayRestParams("input", "a"); // errors as expected
tupleArrayRestParams("input", "a", "b"); // unexpected error! expected this to compile
tupleArrayRestParams("input", "a", "b", "c"); // errors as expected
tupleArrayRestParams("input", "a", "b", "c", "d"); // unexpected error! expected this to compile
tupleArrayRestParams("input", "a", "b", "c", "d", "e"); // errors as expected
tupleArrayRestParams("input", "a", "b", "c", "d", "e", "f"); // unexpected error! expected this to compile
declare const multiTypeTupleArrayRestParams: (
input: string,
...translations: ([string, number])[]
)=> string;
multiTypeTupleArrayRestParams("input", "a"); // errors as expected
multiTypeTupleArrayRestParams("input", "a", 1); // unexpected error! expected this to compile
multiTypeTupleArrayRestParams("input", "a", 1, "c"); // errors as expected
multiTypeTupleArrayRestParams("input", "a", 1, "c", 2); // unexpected error! expected this to compile
multiTypeTupleArrayRestParams("input", "a", 1, "c", 2, "e"); // errors as expected
multiTypeTupleArrayRestParams("input", "a", 1, "c", 2, "e", 3); // unexpected error! expected this to compile📃 Motivating Example
I consider myself a newbie, but given the syntax of the above example, my expected behavior feels more intuitive than the actual behavior. In dynamically typed languages like JavaScript, a repeating sequence of types doesn't seem too unusual. At least, it's common throughout the Clojure API.
However, "a function with a repeating set of parameters" requires using some advanced types to pull off, AFAIK.
💻 Use Cases
- What do you want to use this for? This is come up more than once while writing typescript, and not just for libraries. For example, I wanted to recreate linux's
trcommand with a signature like this:I believe that signature means something different from my intention, therefore some additional syntax may be required.const tr = ( input: string, ...translations: [string, string][] ): string => { return "" }; - What shortcomings exist with current approaches? Additional syntax may be required. It may already be possible to do this and I just don't know the syntax.
- What workarounds are you using in the meantime? This can be achieved with recursive conditional types.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
jcalz commentedon Dec 14, 2023
The thing you're trying to express is a duplicate of #40229
DanKaplanSES commentedon Dec 14, 2023
Thanks, I figured it would've been mentioned already but I didn't find it when I searched.
Is this a complete overlap? I wonder because the examples in #40229 all repeat the same type (
string), whereas I'm requesting a way of repeating sequences of heterogeneous types. In addition, I'm not clear about how #40229 would work syntactically if worked as described as a function rest parameter.I'm assuming the answer is "yes, it completely overlaps," but I figured I'd ask because I might learn something by doing so :)
jcalz commentedon Dec 14, 2023
The types in the example in the other isssue are heterogeneous too; they’re string literal types.
DanKaplanSES commentedon Dec 14, 2023
Oh yeah, I didn't notice that. Thanks again.