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

How to make return type dependent on child type? #6897

Open
maxsalven opened this Issue Sep 15, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@maxsalven

maxsalven commented Sep 15, 2018

I have a function that can take either of two types, and depending on the type of the argument, it returns a specific type. Is there an appropriate syntax for this? Something like:


type ChildA = {commonFlag: boolean}
type ChildB = {commonFlag: boolean, uniqueFlag: boolean}
type ParentA = { children: Array<ChildA>}
type ParentB = { children: Array<ChildB>}

type Fn = 
  | ((parent: ParentA) => Array<ChildA>)
  | ((parent: ParentB) => Array<ChildB>)

export const getChildrenFromParent: Fn = (parent) => {
  return parent.children.map(child => child)
};

const a: ParentA = { children: [] }
const b: ParentB = { children: [] }

const childrenOfA: Array<ChildA> = getChildrenFromParent(a)
const childrenOfB: Array<ChildB> = getChildrenFromParent(b)

https://flow.org/try/#0FAFwngDgpgBAwgCwJYBsAmBBGBeGBvAYwHsBbEogOwDEUBDAcwC4YAjIolKWigX1EliJUaAEI58xMpRoNmbDlwoAaGAFcKSAI6qoMpq3adufcNBgAFWgCcoFEFlx4YBZOhsVmGK1dpgAPELoGAB8JgIW1rYgYo7Ormjunt6+AfEiocD8ZlQU4sAwMAA+MAAUJRCRdsyW7vYAlDjBMF4+-oGYwXX5RaXllSDV-SIN2E0tKe3pXcBQAB4QRFYgzpQAzsv0UCDt7lRWpDVRzDnifbUjTXjdNiCqVrkVtQB0LsLuTyS0ECWv6I1xwi6PAA3JliBR1jBaINag58AC3LZmABtAC6MD44MhLBhURi8N+CSRMDRGLBa2WhPcAHkAGYYJKtVLCELiTbbeK7fYkQ52Eq0LpYymc2x0kSMiZpJq4dk7Wx7A79EosOpAA

@AugustinLF

This comment has been minimized.

Show comment
Hide comment
@AugustinLF

AugustinLF Sep 15, 2018

Contributor

Usually, for function, you want to use the intersection &, not the union |, since the function is of both type, starting with the one the most specific.

So this would give something like that.

You'll observe that added exact types, and made the arrays read-only, without that, you'll get more errors.

However, I still do have errors here, and I'm not sure about how to fix them^^ But the types of the function are correct, flow struggles with its implementation. So that would let you put a $FlowFixMe at the implementation, while still getting your call-sites typechecked.

Contributor

AugustinLF commented Sep 15, 2018

Usually, for function, you want to use the intersection &, not the union |, since the function is of both type, starting with the one the most specific.

So this would give something like that.

You'll observe that added exact types, and made the arrays read-only, without that, you'll get more errors.

However, I still do have errors here, and I'm not sure about how to fix them^^ But the types of the function are correct, flow struggles with its implementation. So that would let you put a $FlowFixMe at the implementation, while still getting your call-sites typechecked.

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