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

Can't pick a property from an disjoint union #8661

Open
danielo515 opened this issue May 12, 2021 · 0 comments
Open

Can't pick a property from an disjoint union #8661

danielo515 opened this issue May 12, 2021 · 0 comments

Comments

@danielo515
Copy link

I have a function with the following signature:

() =>
        | {|
            something: {
              isConnected: boolean,
            },
            isLoading: boolean
          |}
        | {|
            something: null,
            isLoading: boolean
          |}
    >

As you can see, it can return two possible objects, one with a something property which has content and that same objet with null in that property.
I want to use some utility types to pick the non null version of the something property. I tried the following:

type Something = $NonMaybeType<
  $PropertyType<
    $Call<
      () =>
        | {|
            something: {
              connectionError: string,
            },
            isLoading: boolean
          |}
        | {|
            something: null,
            isLoading: boolean
          |}
    >,
    "something"
  >
>

But when I pass any object type with the generated type I still get errors telling me that empty (the null version) is not compatible with object (the thing the function is expecting).

How can I pick the right property of the right side of the sum type?
It can be seen how flow picks the property from the first value of the enum because if I try to nest picks (or elementypes) it says that null does not have any property:

type BambooIntegration = $NonMaybeType<
  $ElementType<
  $ElementType<
    $Call<
      () =>
        | {|
            something: {
              isConnected: boolean,
            },
            isLoading: boolean
          |}
        | {|
            something: null,
            isLoading: boolean
          |}
    >,
    "something"
  >,{}>
>

// => 18:   >,{}>
        ^ Cannot instantiate `$ElementType` because null [1] does not have properties. [incompatible-use]
References:
13:             something: null,
                           ^ [1]

Expected behavior

I expect to get a sum type of the nested property or being able to choose which side of the sum type I want to pick

Actual behavior

It picks the first value (in this case null) from the sum type

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

1 participant