admin管理员组文章数量:1303374
I have the following code, which should refine the example
variable's type using JavaScript's in
operator:
type Example = 'foo' | 'bar' | 'baz';
const objectWithSomeExampleKeys = {
foo: 'foo',
baz: 'baz'
};
function heresTheProblem(example: Example): void {
if (example in objectWithSomeExampleKeys) {
objectWithSomeExampleKeys[example];
}
}
But instead, I get the following error:
10: objectWithSomeExampleKeys[example];
^ Cannot get `objectWithSomeExampleKeys[example]` because property `bar` is missing in object literal [1].
References:
3: const objectWithSomeExampleKeys = {
^ [1]
How do I get Flow to recognize that example
cannot be bar
or any other property not in objectWithSomeExampleKeys
?
I have the following code, which should refine the example
variable's type using JavaScript's in
operator:
type Example = 'foo' | 'bar' | 'baz';
const objectWithSomeExampleKeys = {
foo: 'foo',
baz: 'baz'
};
function heresTheProblem(example: Example): void {
if (example in objectWithSomeExampleKeys) {
objectWithSomeExampleKeys[example];
}
}
But instead, I get the following error:
10: objectWithSomeExampleKeys[example];
^ Cannot get `objectWithSomeExampleKeys[example]` because property `bar` is missing in object literal [1].
References:
3: const objectWithSomeExampleKeys = {
^ [1]
How do I get Flow to recognize that example
cannot be bar
or any other property not in objectWithSomeExampleKeys
?
2 Answers
Reset to default 4I found a solution to this problem:
If I explicitly type objectWithSomeExampleKeys
from my sample code with {[example: Example]: string}
, the error goes away:
type Example = 'foo' | 'bar' | 'baz';
// Explicit type added to objectWithSomeExampleKeys:
const objectWithSomeExampleKeys: {[example: Example]: string} = {
foo: 'foo',
baz: 'baz'
};
function heresTheProblem(example: Example): void {
if (example in objectWithSomeExampleKeys) {
objectWithSomeExampleKeys[example];
}
}
https://flow/try/#0C4TwDgpgBAogHgQwLZgDbQLxQOQDMD2+2UAPjgEYIBOxZ2lAXtgNwBQrAxvgHYDOwUfOQBWEDsADqAS2AALAMr4kEeMjQQA0hBC8AXFADeAbQiIU6favMQAuvv5Up3AOYBfKFgOsoUAvn14hNgANN5QjAGM2KyubKy4AK7c4lI8ULIQVBC8ACoZAApUQuhIABSmahawZuoAlPoAbvhSACaGYVK4UOU16FBOgiJikjIKSiq9mtq8te0+PkKi4tJyispW6lo6JpM2bD6uMUA
The issue is that example can be 'bar'
because Example is of type 'foo' | 'bar' | 'baz'
. This checks out no problem: https://flow/try/#0C4TwDgpgBAogHgQwLZgDbQLxQOQDMD2+2UAPjgEYIBOxZ2lAXtgNwBQrAxvgHYDOwUfOQBWEDsADqAS2AALAMr4kEeMjQQA0hBC8oWAN6soUAvgBcOU9gA0RqIwv0ETW8cpVH77KwC+bVrgArtziUjxQshBUELwAKpEAClRC6EgAFBCIKOgW-FRS3ADmAJQWAG74UgAmUIbGUrhQGVnqUAWCImKSMgpKKi3oWjrFtXbGQqLi0nKKyqrZmtq8ANqZaugAumzGPr5AA
type Example = 'foo' | 'bar' | 'baz';
const objectWithSomeExampleKeys = {
foo: 'foo',
baz: 'baz',
bar: 'bar'
};
function heresTheProblem(example: string): void {
if (example in objectWithSomeExampleKeys) {
objectWithSomeExampleKeys[example];
}
}
本文标签: javascriptFlow Cannot getbecause propertyis missing in object literalStack Overflow
版权声明:本文标题:javascript - Flow: Cannot get ... because property ... is missing in object literal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741726940a2394664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论