以下代码:
/* @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
}
}
产生这些错误:
19: console.log(action.vins)
^ property `vins`. Property not found in
19: console.log(action.vins)
^ object type
不确定本例中我的不相交联合设置有什么问题。
最佳答案
您忘记了从 case 分支内返回
或中断
:
/* @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;
}
}
关于javascript - 在不相交联合类型中找不到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47097503/