Imagine you have an object
{
num1: 1,
num2: 2,
str: "abc"
}
Your goal is to develop a function that accepts any similar object as the first parameter, and a custom selectors object as the second parameter.
function fn<O extends object, S extends object>(
obj: O,
selectors: {
[K in keyof S]: {
selector: (obj: O) => InferReturnType;
equals: (a: InferReturnType, b: InferReturnType) => boolean;
}
}
) {
}
The objective is to automatically deduce the parameters a
and b
of the equals
function based on the return type of the selector
.
For instance,
fn(
{ num1: 1, num2: 2, str: "abc" },
{
selector1: {
selector: ({ num1, num2 }) => ({ num1, num2 }),
equals: (a, b) => a.num1 === b.num1 && a.num2 === b.num2,
},
selector2: {
selector: ({ num1, str }) => ({ num1, str }),
equals: (a, b) => a.num1 === b.num1 && a.str === b.str,
}
}
)