facebook / facebook/flow

narrower type inference of for..in loop variables

Offen
#1,736 16 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.