【发布时间】:2019-06-18 16:09:23
【问题描述】:
我有以下代码,应该使用 JavaScript 的 in 运算符优化 example 变量的类型:
type Example = 'foo' | 'bar' | 'baz';
const objectWithSomeExampleKeys = {
foo: 'foo',
baz: 'baz'
};
function heresTheProblem(example: Example): void {
if (example in objectWithSomeExampleKeys) {
objectWithSomeExampleKeys[example];
}
}
但是,我收到以下错误:
10: objectWithSomeExampleKeys[example];
^ Cannot get `objectWithSomeExampleKeys[example]` because property `bar` is missing in object literal [1].
References:
3: const objectWithSomeExampleKeys = {
^ [1]
如何让 Flow 识别 example 不能是 bar 或不在 objectWithSomeExampleKeys 中的任何其他属性?
【问题讨论】:
标签: javascript flowtype