【发布时间】:2020-06-22 15:01:57
【问题描述】:
const actions = {
setF1: (a: number) => ({
type: 'a',
a
}),
setF2: (b: string) => ({
type: 'b',
b
})
};
type ActionTypes = ?
export default reducer = (state = {}, action: ActionTypes) => {
switch (action.type) {
case 'a':
console.log(`${action.a} is a number`);
return {
...state,
a
}
case 'b':
console.log(`${action.b} is a string`);
return {
...state,
b
}
default:
return state;
}
};
目标是每次我向actions 对象添加一个函数时,reducer 都会自动推断switch 语句中的操作返回类型。我试图避免我需要做类似action: Action1 | Action2 | Action3 的事情。
【问题讨论】:
-
您应该使用
enum而不是字符串文字
标签: typescript typescript-typings