facebook / facebook/flow

Comparators not checking enum validity

Offen
#3,553 11 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

I'm not sure whether this is a feature or a bug, but I found the following behaviour a little surprising:

```js
// @flow

type ItemType =
'FOO'
| 'BAR'
| 'BAZ';

type Item = {|
itemType: ItemType,
value: string
|};

const item: Item = {
itemType: 'FOO',
value: 'test'
};

// why is this ok?
if (item.itemType == 'bar') {
console.log('apparently this is ok');
}

if (item.name === 'Bob') {
console.log('Apparently my name is Bob');
}
```

Is there a reason that a comparison of `item.itemType` with the value `bar` is considered OK? I'm guessing if the functionality is intentional it's to cater for checking code that might be passed as an input to the flow code that has come from a non-type checked source.

If this is the case, that's ok, but it does have a couple of pretty major drawbacks:

1. When refactoring the literal values within for `ItemType` (i.e. removal, case change) flow doesn't complain in these instances.

2. When refactoring an attribute name on the object type (e.g. `itemType` => `type`) this also isn't reported. As shown in the second if statement it's also possible to query an attribute that does not exist on an exact object type (which is also surprising IMO) - happy to submit a separate issue for this if this is also a bug.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

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