Flow具有 keys ,让您说出类似的内容:

   const countries = {
    US: "United States",
    IT: "Italy",
    FR: "France"
   };
   type Country = $Keys<typeof countries>;
   const italy: Country = 'IT';
但是,如果我想拥有valuesCountry之一,则找不到正确的方法。
我想要类似的东西:
function getCountryPopulation(country: $Values<typeof countries>){
...
}
getCountryPopulation(countries.US) //fine
getCountryPopulation("United States") //fine
getCountryPopulation("Canada") //fail

最佳答案

$Values landed in `@0.53.1

用法per vkurchatkin:

const MyEnum = {
  foo: 'foo',
  bar: 'bar'
};

type MyEnumT = $Values<typeof MyEnum>;

('baz': MyEnumT); // No error

有关更多上下文:
https://github.com/facebook/flow/issues/961

09-11 23:57