One of the challenges I'm facing at work relates to a specific function. The function in question is the following:
function spreadCall(f1, f2) {
const args = f1();
f2(...args);
}
The issue is that we need f2 to accept an exact number of arguments (a tuple) in the same order as the output of f1. I've been struggling to find a solution for this problem. One approach I've thought of is to define different numbers of arguments like so:
type Output<F> =
F extends ()=>[infer A0] ? (a0:A0)=>void :
F extends ()=>[infer A0, infer A1] ? (a0:A0, a1:A1)=>void :
F extends ()=>[infer A0, infer A1, infer A2] ? (a0:A0, a1:A1, a2:A2)=>void :
never;
// Add more argument definitions as needed ^^
function spreadCall<InputF>(f1:InputF, f2:Output<InputF>) {
// Your implementation here...
}
The big question remains - is there a way to make it work with any number of arguments automatically?