Given a function type F, I want to create a new type that's "composable before" F, meaning it takes and returns the same arguments that F takes. For example:
const stringToNumber = (s: string) => s.length;
const stringToString: ComposableOn<(s: string) => number> = (s: string) => s + s;
const composedResult = stringToNumber(stringToString('a'));
I've not been able to properly define the type ComposableOn, though. Here are things I've tried:
type ComposableBefore<F extends (...args: any) => any> = (args: Parameters<F>) => Parameters<F>;
type StoN = (s: string) => number;
const sToS: ComposableBefore<StoN> = (s: string) => s + s; // error: the type is [string] => [string] and not string => string
type ComposableBefore<F extends (...args: any) => any> = Parameters<F> extends Array<infer U>? (...args: Parameters<F>) => U: never;
const complex: ComposableBefore<(a: string, b: number, c: { d: number, e: string }) => number> = (a, b, c) => c; // not good either, since it can return a value of any type of the original function's argument types.
What would be a correct way to type this?
const sToS: ComposableBefore<StoN> = (s: string) => [s + s]; const sToN: StoN = (s) => s.length; const res = sToN(...sToS('a'));type ComposableBefore<F extends (...args: any) => any> = (...args: Parameters<F>) => Parameters<F>;