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

Object destructuring breaks disjoint union type. #3932

Open
AndrewSouthpaw opened this issue May 13, 2017 · 4 comments
Open

Object destructuring breaks disjoint union type. #3932

AndrewSouthpaw opened this issue May 13, 2017 · 4 comments

Comments

@AndrewSouthpaw
Copy link

As an example:

type Props =
  | { type: 'foo', item: { id: number, parentId: number | null } }
  | { type: 'bar', item: { id: number, parentId: number } }

function test(props: Props) {
  // This works
  const yay = props.type === 'foo'
    ? props.item.id
    : `/parents/${props.item.parentId}/items/${props.item.id}`

  // This does not
  const { type, item } = props
  const boo = type === 'foo'
    ? item.id
    : `/parents/${item.parentId}/items/${item.id}`
}

I'm running v0.46. Curiously, the try flow REPL doesn't exhibit an error, I'm only getting it locally.

Any ideas?

@vkurchatkin
Copy link
Contributor

This shouldn't really work. When you destructure a union, types get flattened.

@AndrewSouthpaw
Copy link
Author

Hmm... I guess that makes sense. Thanks for pointing that out. I imagine it would be hard/impossible to keep the union with destructuring?

@iddan
Copy link

iddan commented Apr 4, 2018

@vkurchatkin Why is that?

@villesau
Copy link
Contributor

Some related issues:
#6146
#6805
#6594
#6408
#5745
#4772

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

No branches or pull requests

5 participants