Flow: Cannot get ... because property ... is missing in object literal
I found a solution to this problem:
If I explicitly type objectWithSomeExampleKeys
from my sample code with {[example: Example]: string}
, the error goes away:
type Example = 'foo' | 'bar' | 'baz';
// Explicit type added to objectWithSomeExampleKeys:
const objectWithSomeExampleKeys: {[example: Example]: string} = {
foo: 'foo',
baz: 'baz'
};
function heresTheProblem(example: Example): void {
if (example in objectWithSomeExampleKeys) {
objectWithSomeExampleKeys[example];
}
}
https://flow.org/try/#0C4TwDgpgBAogHgQwLZgDbQLxQOQDMD2+2UAPjgEYIBOxZ2lAXtgNwBQrAxvgHYDOwUfOQBWEDsADqAS2AALAMr4kEeMjQQA0hBC8AXFADeAbQiIU6favMQAuvv5Up3AOYBfKFgOsoUAvn14hNgANN5QjAGM2KyubKy4AK7c4lI8ULIQVBC8ACoZAApUQuhIABSmahawZuoAlPoAbvhSACaGYVK4UOU16FBOgiJikjIKSiq9mtq8te0+PkKi4tJyispW6lo6JpM2bD6uMUA