facebook / facebook/flow

Type refinement isn't detected when value is assigned to a variable first

Open
#4,874 0 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

An example speaks louder than words:

```js
type Err = {
status: 'Err',
msg: string,
}

type Ok = {
status: 'Ok',
data: number,
}

type Result = Err | Ok

const result: Result = JSON.parse('{"status": "Ok", "data": 5}')

// this works
if (result.status === 'Ok') {
console.log(result.data * 2)
}

// But this doesn't work!
const status = result.status
if (status === 'Ok') {
console.log(result.data * 2)
}
```

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.