Skip to content Skip to sidebar Skip to footer

Flowtype: Disjoint Union Differentiation Doesn't Work If Types Don't Have A Matching Field

I have a function which should handle an object it receives as a parameter differently depending on whether a field exists on this parameter. I created the following example (try

Solution 1:

You have to use exact types. It is stated further below in the section about disjoint unions with exact type.

Exact types with | have to be added and the if/else has to turn into an if/else if. This code now does not throw any errors anymore (try the new code):

// @flowtype Success = {| value: boolean |};
type Failed  = {| error: string |};

type Response = Success | Failed;

function handleResponse(response: Response) {
  if (response.value) {
    var value: boolean = response.value;
  } elseif (response.error) {
    varerror: string = response.error; // Error!
  }
}

Thanks @user11307804 for pointing me to this answer!

Post a Comment for "Flowtype: Disjoint Union Differentiation Doesn't Work If Types Don't Have A Matching Field"