facebook / facebook/flow

Comparators not checking enum validity

Aperta
#3,553 11 commenti 2 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

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.