facebook / facebook/flow

narrower type inference of for..in loop variables

Aperta
#1,736 16 commenti 1 reazione 0 assegnatari Vedi su GitHub
bug
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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`?

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.