When trying to use mapped tuples as rest parameters error 'A rest parameter must be of an array type' given
See original GitHub issueTypeScript Version: 3.2
Search Terms: mapped tuples rest
Code
type FuncParams<T> = T extends (...args: infer P) => any ? P : never;
type Stringify<T> = {
[K in keyof T]: string;
};
type Optional<T> = {
[K in keyof T]?: T[K];
};
type ThreeParamFunc = (paramOne: string, paramTwo: number, paramThree: boolean) => void;
type Params = FuncParams<ThreeParamFunc>; // [string, number, boolean]
type StringParams = Stringify<FuncParams<ThreeParamFunc>>; // [string, string, string]
type OptionalParams = Optional<FuncParams<ThreeParamFunc>>; // [string?, number?, boolean?]
function doStuff<T>(func: T, ...params: FuncParams<T>) { // works fine
}
function doOptionalStuff<T>(func: T, ...params: Optional<FuncParams<T>>) { // A rest parameter must be of an array type.
}
function doStringStuff<T>(func: T, ...params: Stringify<FuncParams<T>>) { // A rest parameter must be of an array type.
}
Expected behavior: I should be able to use a mapped tuple as a rest param in a function
Actual behavior:
I get the error A rest parameter must be of an array type.
Playground Link: Link
Issue Analytics
- State:
- Created 5 years ago
- Reactions:22
- Comments:13 (4 by maintainers)
Top Results From Across the Web
A rest parameter must be of an array type - Stack Overflow
I have an odd error where the code below works, however the compiler is giving me an error. I believe C in ...args:...
Read more >Rest parameters - JavaScript - MDN Web Docs
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic ......
Read more >Rest parameters and spread syntax
Rest parameters and spread syntax. Many JavaScript built-in functions support an arbitrary number of arguments. For instance: Math ...
Read more >A spread argument must either have a tuple type or be passed ...
A spread argument must either have a tuple type or be passed to a rest parameter. I am trying to do this, but...
Read more >Tuple Types in TypeScript - geekAbyte
It will also touch on generic rest parameters and variadic tuple ... but arrays should contain the same types, and function arguments do...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d be interested to hear why this is marked as a suggestion rather than a bug. As far as I see it you can usually use a tuple type as a function parameter but in this scenario (and other similarly complicated scenarios) it doesn’t work.
Can anyone explain why it doesn’t work in this case? I have tested my mapped type with function parameters and on it’s own it works fine as a rest param:
playground
Having the same problem - prepared a simpler repro (with artificial code ofc).
Real world use case would be to cover reselect’s createSelector API in generic manner - https://github.com/reduxjs/reselect#createselectorinputselectors--inputselectors-resultfunc