facebook / facebook/flow

narrower type inference of for..in loop variables

未关闭
#1,736 16 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Rust
星标
22.3k
派生
1.9k
PR 合并指标
30 天内没有已合并 PR

描述

Consider the following code:

``` javascript
type RawGoodType =
| "Coal"
| "Iron Ore"
| "Copper Ore"
| "Iron Bar"
| "Copper Bar";

type Bundle = {[good: RawGoodType]: number};

class Producer {
recipe: Bundle;
output: Bundle;

canProduce(input: Bundle): boolean {
for (let good in this.recipe) {
if (!input.hasOwnProperty(good) || input[good] < this.recipe[good]) {
return false;
}
}
return true;
}
}
```

This produces the output:

```
src/RawGood.js:18
18: if (!input.hasOwnProperty(good) || input[good] < this.recipe[good])
^^^^ string. This type is incompatible with
10: type Bundle = {[good: RawGoodType]: number};
^^^^^^^^^^^ string enum

src/RawGood.js:18
18: if (!input.hasOwnProperty(good) || input[good] < this.recipe[good])
^^^^ string. This type is incompatible with
10: type Bundle = {[good: RawGoodType]: number};
^^^^^^^^^^^ string enum
```

Which is saying that this fails to type check on `input[good]` and `this.recipe[good]` saying that the type of `good` (`string`) is incompatible with type `string enum`. Shouldn't Flow infer that `good` is a member of the `RawGoodType` string enum since it is a property of `recipe` which is an object whose properties must belong to `RawGoodType`?

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。