facebook / facebook/flow

Problem with simple object destructuring and refinement

Open
#5,259 6 comments 4 reactions 0 assignees View on GitHub
feature request
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Flow seems to not understand correctly object destructuring, which seems super odd to me, or I'm missing something simple. This example:

```ES6
// @flow
function someFunction(obj: { prop?: { subProp: string } }) {
if (!obj.prop) {
return;
}

const { prop: { subProp } } = obj;
return subProp;
}
```

Gives these flow errors:

```
Error: test.js:7
7: const { prop: { subProp } } = obj;
^^^^^^^ property `subProp`. Property cannot be accessed on possibly undefined value
7: const { prop: { subProp } } = obj;
^^^^ undefined
```

But if I just use dot notation to access the sub-prop, flow is happy, like this:

```ES6
// @flow
function someFunction(obj: { prop?: { subProp: string } }) {
if (!obj.prop) {
return;
}

const subProp = obj.prop.subProp; // <-- works! ¯\_(ツ)_/¯
return subProp;
}
```

See: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swBnOAWwFMAxHA47ACjgCMArALjAG8wADgCc4AgPw9+ZTGwAKIgTzL4hhbAHMwAX20BKPqjBhCUMMwCE7DgDpho-b0NGwQ6vkxDsAbidb0R3BJlPkEFSXIZeVFtGIBeMCsfLSA

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.