narrower type inference of for..in loop variables
- Lenguaje dominante
- Rust
- Estrellas
- 22.3k
- Forks
- 1.9k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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`?
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.