Skip to content

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

Open
@danielo515

Description

@danielo515

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions