Problem with simple object destructuring and refinement
- 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
Assessment
This issue has not been assessed yet.