I have typed an object that I want to iterate through and do something to each value and then reconstruct the object without losing its typing and want to do this without mutating anything.
This is what I am trying right now:
type myObj = {
arr1: number
arr2: number
}
const c: myObj = {arr1: 1, arr2: 1}
const d: myObj = Object.entries(c)
.map(entry => {
const prop = entry[0]
const value = entry[1]
return {[prop]: value} // do something to value here
})
// recombine all the objects into the arrObj structure
.reduce((a, e) => {return {...a, ...e}}, {})
d
would have the correct property names and value types and of myObj
, yet typescript gives the error
Type '{ [x: string]: number; }' is missing the following properties from type 'myObj': arr1, arr2
How can I modify this?