Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union Array types in flow

I have the following types:

type formElement = {
  name: string,
  dirty: boolean,
  valid: boolean,
  Errors: Array<inputErrorType>,
  Children: Array<inputElement> | Array<formElement>
}

type inputElement = {
  name: string,
  value: string,
  dirty: boolean,
  valid: boolean,
  Errors: Array<inputErrorType>
};

Then i want to declare an object of those types.

var topForm: formElement = {
  name: "MainForm",
  type: "Form",
  dirty: false,
  valid: true,
  Errors: [],
  Children: [
     { 
      name: "First",
      value: "test",
      dirty: true,
      valid: true,
      Errors: []
    }
  ]
}

And I am getting an error that says that:

Could not decide which case to select union type

Obviously since i am missing the "Children" property, my item in the array should be an inputElement.

like image 791
Aflred Avatar asked Mar 13 '26 20:03

Aflred


1 Answers

Children: Array<inputElement> | Array<formElement>

I needed to change to

 Children: Array<inputElement | formElement>

Hence the array can have both inputElement or formElement not one of the either.

like image 122
Aflred Avatar answered Mar 16 '26 10:03

Aflred



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!