从流程文档我们有这个:
// @flow
const countries = {
US: "United States",
IT: "Italy",
FR: "France"
};
type Country = $Keys<typeof countries>;
const italy: Country = 'IT';
const nope: Country = 'nope'; // 'nope' is not a Country
但是我想做
type CountryValue = $Values<typeof countries>
const italy: CountryValue = 'Italy'; // yes
这可能吗?
您可以使用$Values
,但您还需要做更多的事情,因为目前内部的值countries
只是被检测为 any string
。如果您告诉 flow 只允许某些值,那么它可以工作:
type FullNames = "United States" | "Italy";
const countries: {[key: string]: FullNames} = {
US: "United States",
IT: "Italy"
};
const nope: $Values(typeof countries) = 'nope'; // 'nope' is not in the value type
我猜想这是不是很想要的东西,因为它需要显式地添加类型,但它是可行的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句