facebook / facebook/flow

variable refinement of a union

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

Description

When I run the following code through flow:

```js
type Action = { type: 'ACTION_1', thing1: string } | { type: 'ACTION_2', thing2: string }
function handleType(type: string, callback: (action: Action) => void) {
return function(action: Action) {
if (action.type === type) {
callback(action)
}
}
}

handleType('ACTION_1', (action: Action) => console.log(action.thing1))
```

I get the following error:

```
property `thing1` Property not found in object type
```

Is there a way to get Flow to recognize that the action has been refined to the first member of the union within my callback?

Here's the above example on [flowtype.org/try](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCCAxhgJZwB2YAvGAN5jb4BcYA5IQMIAqAkgHkAcgH0AjOwA0TABZlKAczFsAzhgBO8hWAC+YAD4MmuPG069BogExTZWq6o1bdqKAFdKpCtRkBDSgAmMHg8JgAUzKZgapqK0sS+MDAARr7EANZsYWnkVGwkuZQAlLQAfGAAbnBkASX0qGBg6ngYburU7p6F2V55RL3FDA2NYGRQYD2FAHSRtDR0kXXDI2AJSakZk95Fyy6NOqgHqH6BwaH4Yeb8wuK2W30F22WrVCpwwVPwCveUM3KKYiKRSAA)

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.