I'm trying to use reduce to create a dictionary from an array. However I run into a flow error property is missing in object literal [1] when using reduce to create a dictionary from an array. This happens for all three keys 'A', 'B', and 'C'.
const foo = [
'A',
'B',
'C',
];
const bar = foo.reduce((acc, curr) => {
// the actual code will do some logic on the string value in foo
// but for the test just create a dictionary of the key to itself as value
acc[curr] = curr;
return acc;
}, {});
The problem here is that Flow recognizes the initial value
{}as being just that - an empty object. So you trying to add something to{}.Athrows the warning/error.Here is a solution, giving the initial value a custom Dictionary type, see Objects as Maps:
PS: This works the same in Typescript