TS Error:
Type '(string | undefined)[]' cannot be used as an index type.ts(2538)
Code:
Object.values(payload.reduce((acc, obj) => {
let key = obj.values.map(i => i.object_id);
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, {}))
Works fine in the javascript code. Why?
keyinto a string whenkeyis used as an object property. TypeScript probably doesn't like this for whatever reason. You can likely resolve this problem by explicitly doingkey = key.toString();to stop TypeScript from complaining.i => i.object_id!). This led to an error "Type 'string[]' cannot be used as an index type.ts(2538)"