I would like to type the following function:
carryOver = <T extends Record<string, any>>(fun: (obj: T) => Record<string, any>) => (obj: T) => Object.assign({}, obj, fun(obj))
Such that I can do e.g.:
const makeGreeting = ({name, title}) => ({greeting: `Hello ${title} ${name}`})
const person = {name: "Thomas", title: "Dr."}
const personWithLabel = carryOver(makeGreeting)(person)
// { name: "Thomas", title: "Dr.", greeting: "Hello Dr. Thomas" }
I.e. carryOver() accepts a function that takes in an object A, destructures it, and returns a new object B, with properties computed from the destructured properties of A. carryOver() then combines the objects together, so we end up with augmented object with properties from both A and B.
How do I type carryOver() so that the type of the output has both the props of A and B?