[英]How can i have a interface check in typescript switch statements?
I am using Typescript and redux我正在使用 Typescript 和 redux
I want to compare passed action with an interface and then perform the task我想将传递的操作与接口进行比较,然后执行任务
export function counterReducer(
state = initialState,
action: CounterActionTypes
): CounterState {
switch (action) {
case IncrementAction:
return { ...state, count: state.count + action.incBy };
case action as DecerementAction:
return { ...state, count: state.count + action.decBy };
default:
return state;
}
}
Maybe you can check into type guards for that.也许您可以为此检查类型警卫。
https://www.typescriptlang.org/docs/handbook/advanced-types.html https://www.typescriptlang.org/docs/handbook/advanced-types.html
I would probably use redux-toolkit
or redux-actions
for that because it simplifies a lot your code and it has Typescript support我可能会为此使用
redux-toolkit
或redux-actions
,因为它大大简化了您的代码,并且支持 Typescript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.