What is the best way to cast the action
parameter in a redux reducer with typescript? There will be multiple action interfaces that can occur that all extend a
I might be late to the dance but enum
's FTW!
enum ActionTypes {
A: 'ANYTHING_HERE_A',
B: 'ANYTHING_HERE_B',
}
interface IActionA {
type: ActionTypes.A;
a: string;
}
interface IActionB {
type: ActionTypes.B;
b: string;
}
type IAction = IActionA | IActionB
const reducer = (action: IAction) {
switch (action.type) {
case ActionTypes.A:
return console.info('action a: ', action.a)
case ActionTypes.B:
return console.info('action b: ', action.b)
}
}