I have a Flow type defined as
export type fruit = 'apple' | 'orange' | 'pear';
What I want to do is validate a string and see if it matches any of the values defined in this type.
Is there a way to do this?
The best approach is to use an array then validate by using Array#includes like this.
const fruits = ['apple', 'orange', 'pear'];
const isValid = anyFruit => fruits.includes(anyFruit);
console.log(isValid("apple"));
console.log(isValid("orange"));
console.log(isValid("pear"));
console.log(isValid("banana"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With