I have an array with the values:
const currencies = ['USD', 'BRL', 'EUR']
// The true array has more then 15 currencies values
I want to avoid doing this to avoid code duplication:
type Currencies = 'USD' | 'BRL' | 'EUR'
Is it possible to use those values as types? I tried to do that:
type Currencies = $Values<currencies>
But I got an error because $Values are only for objects.
What is the best way of not duplicating the code since I already have those values in an array.
As far as I can tell there isn't a way to do this currently with flow using an array.
One approach is to turn your array into an object and use $Keys with typeof
const currencies = ['USD', 'BRL', 'EUR'];
const currenciesObj = currencies.reduce((agg, next) => ({...agg, [next]: true}), {});
type Currencies = $Keys<typeof currenciesObj>;
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