narrower type inference of for..in loop variables
- Dominant language
- Rust
- Stars
- 22.3k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
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`?
Contributor guide
Assessment
This issue has not been assessed yet.