You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular13 NgRx13配置ActionReducerMap出现TS2322类型错误怎么解决?

错误根因

NgRx 定义的 ActionReducer 类型要求 reducer 函数的第一个 state 参数允许接收 undefined 类型:应用初始化阶段 NgRx 会传入 undefined 触发 reducer 返回初始状态,你当前定义的 commonReducer 仅接收必填的 CommonState 类型,严格模式下 TypeScript 就会抛出类型不匹配错误。

解决方案

仅需要修改 common.reducer.ts 中导出的 commonReducer 函数,给 state 参数增加默认值同时兼容 undefined 类型即可:

// 修改前
// export function commonReducer(state: CommonState, action: Action) {
//   return _commonReducer(state, action);
// }

// 修改后
export function commonReducer(state: CommonState | undefined = commonState, action: Action) {
  return _commonReducer(state, action);
}

修改后重新编译即可解决该报错。


内容的提问来源于stack exchange,提问作者Rene Nochebuena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:15:11