小编jf1*_*234的帖子

TypeScript 和 Redux:为什么我需要添加` | undefined` 到我的 Reducer 状态类型?

为什么我不能像这样简单地输入我的减速器,而不在我的 redux 状态类型中添加“| undefined”?

const reducer: Reducer<ReduxState> = function(state: ReduxState, action: any) ...
Run Code Online (Sandbox Code Playgroud)

额外的上下文::::

这工作正常:

export const reducer: Reducer<ReduxState | undefined> = function (
    state: ReduxState | undefined, 
    action: any
) {
    return state
}
export default reducer
Run Code Online (Sandbox Code Playgroud)

但是,从 fxn 中删除状态参数“未定义”时不会:

export const reducer: Reducer<ReduxState | undefined> = function (
    state: ReduxState, 
    action: any
) {
    return state
}
export default reducer
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误:

Type '(state: ReduxState, action: any) => ReduxState' is not assignable to type 'Reducer<ReduxState | undefined, AnyAction>'.
  Types …
Run Code Online (Sandbox Code Playgroud)

javascript typescript redux

7
推荐指数
1
解决办法
1126
查看次数

标签 统计

javascript ×1

redux ×1

typescript ×1