I want write a TypeScript function that merges one or more transform functions.
A transform function has this signature:
type Transform<T0, T1> = {
(arg: T0): T1 | Promise<T1>;
}
And the merge function has this signature:
type Merge = {
<T0, T1>(f0: Transform<T0, T1>): Transform<T0, T1>;
<T0, T1, T2>(f0: Transform<T0, T1>, f1: Transform<T1, T2>): Transform<T0, T2>;
<T0, T1, T2, T3>(f0: Transform<T0, T1>, f1: Transform<T1, T2>, f2: Transform<T2, T3>):
Transform<T0, T3>;
// ...
};
Is there a way to write that type so that the function can accept any number of argument?
EDIT:
I want to add some example: Consider these functions:
function toNumber(x: string): number {
return parseFloat(x);
}
function toString(x: number): string {
return `${x}`;
}
This call is correct: merge(toNumber, toString)
.
But if we try to call merge(toNumber, toNumber)
, the compiler
should emit an error because, the first argument returns a number and the
second one shall accept a number, not a string.
That is why using any
in the function signature cannot work.