Closed
Description
TypeScript 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