I have a object fn with name:function pairs that can take arbitrary parameters. I want to be able to map these functions in order to wrap them with another default function to handle errors in a single location. I have the following code which doesn't even compile giving A spread argument must either have a tuple type or be passed to a rest parameter. on fn[f](...args)
const fn = {
...userActions,
...companyActions,
...dataActions
};
type A<T extends Array<any>, U> = Record<keyof typeof fn, (...args: T) => Promise<U>>;
function map<T extends Array<any>, U>(f: keyof A<T, U>, ...args: T) {
fn[f](...args);
}
I can wrap functions using the code below but it requires importing the relevant function into the caller file and passing it into fn parameter
async function run<T extends Array<any>, U>(fn: (...args: T) => Promise<U>, ...temp: Parameters<typeof fn>) {
await fn(...temp); // works but needs to pass the function itself.
}
Is there a way to get around the issue on first piece of code? Basically I want to infer the parameter type by using just the key from a record.