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
With Typescript 2's Tagged Union Types you can do the following
interface ActionA {
type: 'a';
a: string
}
interface ActionB {
type: 'b';
b: string
}
type Action = ActionA | ActionB;
function reducer(action:Action) {
switch (action.type) {
case 'a':
return console.info('action a: ', action.a)
case 'b':
return console.info('action b: ', action.b)
}
}