facebook / facebook/flow

Cant Refine Objects as Documented

Open
#6,779 8 comments 0 reactions 0 assignees View on GitHub
Typing: unions/intersections
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

### [Try Flow](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgBiccYAvGAN4A+qY9DjDA1FCQFxgDkbcXANHSbDqAX3TZ8YAEIBDAE7kqtYcNYduAIwVcwQ1QzETcBOQC8lNfQfrMcsrPFkATTgGcM8gJYA7AObWBkboqFAArj4AxhhecD5geAAesgC2ODB4ABRwmgBWnMSk1DIKYMXmAJRUqACQXlBgmZJ4cA05uQB09o5wLmAAhGQUXB7e-lxVlNY12XkFJGUl8hXW4qJAA)

Disjoint Unions do not work when using exact object types as defined

https://flow.org/en/docs/types/unions/#toc-disjoint-unions-with-exact-types

```javascript
/* @flow */

type Foo = {|
+foo: 'foo',
|}

type Bar = {|
+foo: 'bar'
|}

type Baz = {|
+payload: string
|}

function example(obj: Foo | Bar | Baz) {
if (typeof obj.payload !== 'string') {
(obj: Foo | Bar)
}
}
```

in this case it errors because obj could be Baz... but there is actually ZERO possibility in this case it could possibly be Baz. In fact it would appear that it is 100% impossible to refine this object even with it being as strict as can be.

```javascript
function example(obj: Foo | Bar | Baz) {
if (obj.foo === 'foo' || obj.payload === 'bar') {
(obj: Foo | Bar)
}
}
```

also does not work . here

nor does trying to take it a step further

```javascript
function example<+O: Foo | Bar | Baz>(obj: O) {
if (typeof obj.payload !== 'string') {
(obj: Foo | Bar)
}
}
```

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.