Flow has keys
, that lets you say somthing like:
const countries = {
US: "United States",
IT: "Italy",
FR: "France"
};
type Country = $Keys<typeof countries>;
const italy: Country = 'IT';
but if I want to have one of the values
of Country
, I can't find proper way.
I want something like:
function getCountryPopulation(country: $Values<typeof countries>){
...
}
getCountryPopulation(countries.US) //fine
getCountryPopulation("United States") //fine
getCountryPopulation("Canada") //fail
Objects have two types of properties: data and accessor properties.
Flow objects can be separated into three. They are events, activities, and gateways.
The basic properties of an object are those items identified by its four-part name (name, type, instance, and version) and also include owner, status, platform, and release.
$Values
landed in `@0.53.1.
Usage, per vkurchatkin:
const MyEnum = {
foo: 'foo',
bar: 'bar'
};
type MyEnumT = $Values<typeof MyEnum>;
('baz': MyEnumT); // No error
For more context: https://github.com/facebook/flow/issues/961
You could do this with some duplicate code:
type Country = "United States" | "Italy" | "France";
type Countries = {
US: Country,
IT: Country,
FR: Country
}
const countries: Countries = {
US: "United States",
IT: "Italy",
FR: "France"
};
function getCountryPopulation(country: Country) {
return;
}
getCountryPopulation(countries.US) //fine
getCountryPopulation("United States") //fine
getCountryPopulation("Canada") //fail
Related issue: How to use/define Enums with Flow type checking?
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