I have a memoizer that allows you to transform arguments before comparing, something like so:
function sum(x: number, y: number) { return x + y; }
const memoSum = memo(sum, {
// receives an array of numbers ([x, y]) and converts to a string (i.e., `${x},${y}`)
transformArgs: (args) => args.join(','),
// receives the transformed arguments (strings in this case)
isEqual(a, b) => a === b
})
Is there a way to write an interface that infers the type of arguments a
and b
iff transformArgs
is provided? Something like:
interface MemoOptions<F extends AnyFunction> {
transformArgs(args: Parameters<F>): TransformedArgsHereDefaultsToParametersF;
isEqual(a: TransformedArgsHereDefaultsToParametersF, b: TransformedArgsHereDefaultsToParametersF): boolean;
}
Edit: Adding TS playground with the answer. Thanks for helping!
Simply add another generic type parameter:
interface MemoOptions<F extends AnyFunction, R> {
transformArgs(args: Parameters<F>): R;
isEqual(a: R, b: R): boolean;
}