facebook / facebook/flow

Comparators not checking enum validity

Open
#3,553 11 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.