I want to use a function like this:
await checkTasks(
[foo, 123],
[bar, "lol"],
);
Here, foo and bar are functions:
async function foo(arg: number) {}
async function bar(arg: string) {}
I want the checkTasks function to be type safe such that if I try to write something like this:
await checkTasks(
[foo, 123, "someBogusExtraArgument"],
[bar, "lol"],
);
It would give a type error.
My naive attempt at writing this function is as follows:
type Task<T extends (...args: any[]) => any> = [T, ...Parameters<T>];
export async function checkTasks<
T extends [...Array<Task<(...args: any[]) => any>>],
>(...tasks: T): Promise<void> {
for (const [fn, ...args] of tasks) {
await fn(...(args as any));
}
}
However, it does not seem to work properly. Meaning that I do not get a type error on "someBogusExtraArgument".
For reference, here is the code in the TypeScript playground (see link below).
How do I write this function correctly?