I am interested in utilizing the following function:
declare function foo(...args: any): Promise<string>;
The function foo
is a pre-defined javascript 3rd party API call that can accept a wide range of parameters.
The objective is to present a collection of potential function types to be passed into a wrapper function, which will help enforce the parameter types of this function. This generic wrapper function will take a type parameter representing another function and use its parameters and return type to determine the parameters and return type of the actual function invocation.
async function fooWithType<T extends (...args: any) => string>(args: Parameters<T>): Promise<string> {
return await foo(...args)
}
Below is an example of one potential function type definition and how it could aid in invoking this 3rd party function through the wrapper function:
type funType = (a: string, b: boolean, c: number) => void;
fooWithType<funType>(["", true, 4])
However, I encountered an error indicating that I cannot use the spread operator on args?: Parameters<T>
.
Type 'Parameters<T>' must have a '[Symbol.iterator]()' method that returns an iterator.
Why does the spread operator not work as expected here? I assumed I would be able to spread the tuple argument into the vararg parameter of the foo
function.