Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disjoint unions fail when destructuring tagging property #4828

Closed
avaly opened this issue Sep 7, 2017 · 3 comments
Closed

Disjoint unions fail when destructuring tagging property #4828

avaly opened this issue Sep 7, 2017 · 3 comments

Comments

@avaly
Copy link

avaly commented Sep 7, 2017

The following typing triggers an error in the reducerFails reducer where we access the action.type after a destructuring operation. Flow should apply the same logic as it does for the case in reducerPasses.

type Action = 
  | { type: 'FOO' }
  | { type: 'BAR', payload: {} };

function reducerPasses(action: Action) {
  if (action.type === 'BAR') {
    return { ...action.payload };
  }
  return action;
}

function reducerFails(action: Action) {
  const { type } = action;
  if (type === 'BAR') {
    return { ...action.payload };
  }
  return action;
}

See example code

Reference docs: https://flow.org/en/docs/types/unions/#toc-disjoint-unions

@vkurchatkin
Copy link
Contributor

This is expected. At this point type is basically 'FOO' | 'BAR'. Flow doesn't keep track of the fact, that it is somehow related to Action.

@avaly
Copy link
Author

avaly commented Sep 8, 2017

It's weird that Flow can not make the connection between the desctructured type and action.type and figure out what action is at that point.

But if this is the intended use case for disjoint unions and refinements, at least this can be highlighted in the docs section for disjoint unions (I can't find any mention of this requirement), in order to not confuse other folks. @vkurchatkin what do you think?

@mgtitimoli
Copy link

@vkurchatkin is there any near plan to support destructuring for disjoint unions, should a proposal be created for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants