Closed
Description
I'm writing a react/redux project in TypeScript and I would like to have a little different structure of actions and reducers.
Here is the example:
abstract class Action {
type: string;
}
class ActionOne extends Action {
constructor(public one: string) {
super();
this.type = "TypeOne";
}
}
class ActionTwo extends Action {
constructor(public two: number) {
super();
this.type = "TypeTwo";
}
}
and my reducer method
function reducer(state, action: Action) {
if(action instanceof ActionOne) {
action.one; // works
action.two; // error
} else if (action instanceof ActionTwo) {
action.one; // error
action.two; // works
} else {
return state;
}
}
Thank to these changes and to type guard feature I have intellisense and type checking inside of each if/else body.
The only problem with this solution is that dispatch method checks if the action object is the plain object which in my case is not true.
Metadata
Metadata
Assignees
Labels
No labels