Reputation: 82
I'm writing a function that accepts a string, and if the string value is a member of an enum, I'd like to treat it like the type I've defined for that enum. A simplified example:
const FRUITS = Object.freeze({
APPLE: 'APPLE',
BANANA: 'BANANA',
});
type Fruit = $Values<typeof FRUITS>
function fruitChecker(input: string) {
if (Object.values(FRUITS).includes(input)) {
(input: Fruit);
}
}
Flow throws an error saying my input in that conditional is not a Fruit. But how can my input not be a Fruit as I've defined it? Is there a right way to perform operations on an enum member after refining the input from a broader, primitive type?
Upvotes: 1
Views: 194
Reputation: 494
See: https://github.com/facebook/flow/issues/6904 and https://github.com/facebook/flow/issues/2221
So .includes
can't be used for refinement, and Object.values
returns mixed
. This means that the scenario you are trying is not currently possible.
Upvotes: 1