有TAction
这样的类型
export type TAction<TPayload = any> = {
+type: string,
payload?: TPayload
};
在这里,我给它TPayload
(
state: TSettings,
action: TAction<{ key: any, value: any }>
) => ({
...state,
[action.payload.key]: action.payload.value
}),
但是流程给我错误
Cannot get action.payload.key because property key is missing in undefined [1].
答案 0 :(得分:0)
您已将payload
标记为maybe type(问号),因此它可能是null
或undefined
。
删除问号:
export type TAction<TPayload = any> = {
+type: string,
payload: TPayload
};
或添加检查是否已定义payload
,即:
(
state: TSettings,
action: TAction<{ key: any, value: any }>
) => action.payload ? ({
...state,
[action.payload.key]: action.payload.value
}) : {...state},