facebook / facebook/flow

Flow does not understand relationship between object properties after destructuring

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

Description

I'm not sure if this is a bug, room for improvement, or something that cannot reasonably be expected to work. I found it interesting, so I thought I'd bring it up. Feel free to close if this is out of the scope of what flow can reasonably be expected to do.

------------

I am working on a feature where users can select 2 different types of things from a dropdown list. We decided to pass the selected value(s) through the same function, but wrap them in an object that specifies the type. I destructured this wrapper object, and was not able to understand the relationship between the values anymore.

Here's a small reproduction of the issue

```ts
// The 2 selectable types
type TypeUser = {| userid: number, first_name: string, last_name: string |};
type TypeWorkflow = {| workflowid: number, name: string |};

// Wrapper objects that indicate the type that has been selected
type TypeSelectedUserOrWorkflow = {|
type: 'workflow',
value: TypeWorkflow
|} | {|
type: 'user',
value: TypeUser
|};

// After destructuring, Flow no longer understands the relationship between type and value.
function selectUserOrWorkflowBroken(selected: TypeSelectedUserOrWorkflow): number {
const { type, value } = selected;
if (type === 'workflow') {
// Cannot get `value.workflowid` because property `workflowid` is missing in `TypeUser`
return value.workflowid;
} else {
// Cannot get `value.userid` because property `userid` is missing in `TypeWorkflow`
return value.userid;
}
}
```
My first thought was that flow _should_ be able to understand this because I destructured the values into a `const`, but it does also seem very complicated to track the origin of variables through code in order to understand the relationship between them at any time.

https://flow.org/try/#0PTAEBUAsFNQJlAZ2gG2gYwC4EMBGbRMBPAB2kQChiyJToBVZAJ1AF5QBvAH1AFdmAlgBMAXKAB2vALa5oTADSgAZgKaJMAfXHYp0MeqYDxAc0Ups6rTr1JMhk6C4BfANxU6tMgHUA9kwDWSig+AO5snDwhfoHBIcJikjJyitq6+nZGxo6uFBQgoF5M2CRkLD64AFYYmIiEkNiYoEZCAugNsJgwhB6dDaD1tbLQ4kio1dBC7jTgdADKY1gTjHIA8ky+AUGh4dwUoPvdZGIA5FGbscfyewcAbtgovDYz3tFbIRTOjhHX+9Q2x-w5JcfqA7g8nnRlkwPjk8mAoLBZLFQLpOj4hLVsOh0NASI1OrBntB5mhFkIoWsNjFQopsOIhKAmNBMLwmCMCaAAJIAEQAdLl8gAlaBSHw3DqQAS1JS8cRYAQ+cS0+l1WByJh+WrGHygbAhbBEOGgABCAEFuaAALSgABiyKEPnIEh8jVlQjk6jpDI5TPMmAV4kQkpIoB8SkO0GAYMeyg1UlVuvlit5JANwWwDMVOKNRB8vGO4tA7oMvCwrImTUaRkwOruhjwaEQ-Jlcv9itGpMwFPWr1ixo1-mGAApkJ2JmIiSTxuTmJTe6EAJQJaSyFgca7oRXqTgRxTR2BOcKj6dufYCcNDv5sVjsU7zkLHBecEH7Jkstmg+6PXlnalxISnqAh6oMgz4HIyzKsiM+68oChgAdcTgUEhRoAOIrCsFrWl4AhorwjTFnYpbvpkCapkQ6ZCIobx8PSHo4PStS9I0IQwOyXRXlKoB3ucoSXJ+4JNExHi-m8FAtkmIzHlg3ZUm8qE+OiI4LJg46eMSKlLLOPa8SES4SCuchgU0F7SapQi8leN63qJFxPuu4GvpBH5mRMvIwbZoTCIBwEoKBDkHG+UEdtO7lftAsGCAh+xISh+ToZhVpcuGua8EW5BEWWTK6koqksByV7oDA6D+NRyJunIDEYnUfQcvuQmgNAuEwNC+REnJyJ+OpUK6iUGokPWqkoIaEltlJKmyfeqEaogiDKWOojqVOZKTbp+mJKuxnnqA82hVZN7cZ5D72S+oCboGjQcAJMaHuwrnRY5QUfh597eYhjV+bAAX7Od25XfVt0hWSgFOe+0HhZFchvTFyG5EAA

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.