为什么我不能像这样简单地输入我的减速器,而不在我的 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)