I am looking to create a function that can take an array of objects as input and return a new object containing all the attributes from those objects. The code snippet below demonstrates my attempt at this:
function combineAttributes<T extends object>(arr: T[]) {
let obj = {};
arr.forEach((val) => {
obj = { ...obj, ...val };
});
return obj;
}
const c = { color: "green" };
const d = { size: "medium" };
const product = combineAttributes([c, d]);
https://i.sstatic.net/pm1AQ.png
I want 'product' to be a dynamic TypeScript value based on the input, but I am still unsure how to achieve this.