Function to wrap function and provide one property of object

Viewed 84

I want to create a new function from an initial function, with the initial function taking a single object argument and the new function taking the same single object argument apart from populated fields. I've got something like this so far:

function returnNewFunc<TArg extends { b?: string }>(
  initialFunc: (arg: TArg) => void
) {
  const newFunc = (arg: Omit<TArg, "b">) =>
    initialFunc({ ...arg, b: "hello" });

  return newFunc
}

const funcA = ({ b, c }: { b: string; c: string }) => {
  console.log(b, c);
};
const funcB = returnNewFunc(funcA)
funcB({c: "test"}) // funcB should have 'c' as the only field in its argument

The issue here is I get a could be instantiated with a different subtype of constraint typescript error, so I must be doing something wrong.

0 Answers
Related