Is it possible to two reduce objects into one by summing their properties like so for any generic object
const A = {x:1, y:1, z:1}
const B = {x:2, y:2, z:2}
// result {x:3, y:3, z:3}
I am hoping to get some function
function reduceObjects<T extends {[readonly key: string]: number}>(previousObject:T, currentObject:T) => T
when I try this solution
function reduceObjectsGeneric<T extends {readonly [key: string]: number}>(currentValue: T , previousValue: T): T {
const result = Object.assign({}, previousValue);
Object.keys(previousValue).forEach((k) => {
// eslint-disable-next-line functional/immutable-data
result[k]=previousValue[k]+currentValue[k]
})
return result
}
I get the following error in the inner loop
Type 'string' cannot be used to index type '{} & T'.ts(2536)
What is the functional way to implement this behaviour?