我有一个对象包含静态类型的不同属性 . 问题是当我尝试静态访问其中一个属性时,流似乎不理解并返回我想要访问的特定属性的类型 .
这是流量限制还是有办法处理?
这是一个示例代码,展示了我想要实现的目标
/* @flow */
const object = (({
first: 'first',
second: 2,
}): {
first: string,
second: number
});
type ObjectPropertiesType = $Keys<typeof object>;
const getObjectValue = (property: ObjectPropertiesType) => {
if (!object[property]) throw new Error(`object.${property} not exisiting`);
return object[property];
};
// -> Flow here complains that getObjectValue isn't compatible with string but is string | number
const getFirst = (): string => getObjectValue('first');
1 回答
我在github flow repo上回答了问题https://github.com/flowtype/flow-bin/issues/112
这是解决方案:
See it in action