I have a function which effectively merges objects together like so
function merge(...objects) {
let temp = {};
for(const obj of objects) {
for(const key in obj) {
temp[key] = obj[key];
}
}
return temp;
}
What is the correct way to describe the output type such that if for example: objects is of type [{a: number}, {b: string}]
, the output is of type {a: number, b: string}
?
My attempt at this intuitively is as follows:
type _return<T extends any[]> = {
[name in keyof T]: typeof T[name]
};
function merge<T extends any[]>(...objects: T): _return<T> {
let temp: _return<T> = {};
for(const obj of objects) {
for(const key in obj) {
temp[key] = obj[key];
}
}
return temp;
}
I know this is incorrect, but maybe it will serve as a better description of my intentions.
I've taken a look at how lodash types merge objects, and it seems even they have given up
Edit: Typos in code