Given an arbitrary number of objects of different shape, is it possible for TypeScript to properly assert type information?
Example:
const merge = (...objects) => {
return Object.values(objects).reduce((acc, next) => ({ ...acc, ...next }));
}
My actual use case is a lot more involved than this, but the type signature would be similar.
Here's what works, but I hope is not the only solution:
function merge<T1, T2>(...objects: [T1, T2]): T1 & T2;
function merge<T1, T2, T3>(...objects: [T1, T2, T3]): T1 & T2 & T3;
function merge(...objects) {
return Object.values(objects).reduce((acc, next) => ({ ...acc, ...next }));
}
My issues with this are that it fails to compile with the newer versions of TypeScript and it's not at all maintainable.
Is there a better way to achieve this?