Zachary Carter
Zachary Carter

Reputation: 373

Property not found in disjoint union type

The following code:

/* @flow */

type StepApplicationAction = { type: "STEP_APPLICATION" }
type RewindApplicationAction = { type: "REWIND_APPLICATION" }

type ApplicationAction = StepApplicationAction | RewindApplicationAction

type RequestVinsAction = { type: "REQUEST_VINS" }
type ReceiveVinsAction = { type: "RECEIVE_VINS", vins: Array<string> }

type VehicleAction = RequestVinsAction | ReceiveVinsAction

type Action = ApplicationAction | VehicleAction

function reducer(action: Action) {
  switch (action.type) {
    case "REQUEST_VINS": console.log('HERE')
    case "RECEIVE_VINS":
      console.log(action.vins)
    default:
      return
  }
}

Produces these errors:

19:       console.log(action.vins)
                         ^ property `vins`. Property not found in
19:       console.log(action.vins)
                  ^ object type

Not sure what's wrong with my disjoint union setup in this example.

Upvotes: 0

Views: 100

Answers (1)

user6445533
user6445533

Reputation:

You have forgotten to return or break from within the case branch:

/* @flow */

type StepApplicationAction = { type: "STEP_APPLICATION" }
type RewindApplicationAction = { type: "REWIND_APPLICATION" }

type ApplicationAction = StepApplicationAction | RewindApplicationAction

type RequestVinsAction = { type: "REQUEST_VINS" }
type ReceiveVinsAction = { type: "RECEIVE_VINS", vins: Array<string> }

type VehicleAction = RequestVinsAction | ReceiveVinsAction

type Action = ApplicationAction | VehicleAction

function reducer(action: Action) {
  switch (action.type) {
    case "REQUEST_VINS": console.log('HERE'); return;
//                                            ^^^^^^^
    case "RECEIVE_VINS":
      console.log(action.vins); return;
    default:
      return;
  }
}

Try Flow

Upvotes: 2

Related Questions