facebook / facebook/flow

Comparators not checking enum validity

Abierto
#3,553 11 comentarios 2 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Rust
Estrellas
22.3k
Forks
1.9k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.