Reducing Boilerplate of Redux with TypeScript<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Reducing Boilerplate of Redux with TypeScript

Posted on: May 15, 2018

For quite a while, I found that working with TypeScript and Redux was slow in term of all the boilerplate required when adding a new Redux's action. The creation of a unique constant, a unique type to narrow down the type and then an action creator function to have a unique place to call the action. The last step was to create a union of all actions (type) allowed for the Redux's reducer which was passed in the reducer (similar when the action was used in a middleware). With the arrival of TypeScript 2.8, it's easy to reduce the boilerplate.

First, some generic code must be in place.

1export function createActionPayload<TypeAction, TypePayload>(actionType: TypeAction): (payload: TypePayload) => ActionsWithPayload<TypeAction, TypePayload> {
2 return (p: TypePayload): ActionsWithPayload<TypeAction, TypePayload> => { return { payload: p, type: actionType }; };
3 }
5export function createAction<TypeAction>(actionType: TypeAction): () => ActionsWithoutPayload<TypeAction> {
6 return (): ActionsWithoutPayload<TypeAction> => {
7 return { payload: {}, type: actionType };
8 }; }
10export interface ActionsWithPayload<TypeAction, TypePayload> {
11 type: TypeAction;
12 payload: TypePayload;
15export interface ActionsWithoutPayload<TypeAction> {
16 type: TypeAction;
17 payload: {};
20export type ActionsUnion<A extends ActionCreatorsMapObject> = ReturnType<A[keyof A]>;

It might look like a lot of code, but in reality, you won't touch that code at all, just call it. Special mention at the last type which uses a TypeScript 2.8 feature ReturnType which will get all return types of the function we will have in a type removing some manual entries.

At that point, to create a new action consist of few steps but not too many keystrokes.

First, you still need a constant. The constant is of a type of a string literal (not a string) and must be unique by action. This will be used later to narrow the type down to the payload of the action itself.

1export const ACTION_1 = "ACTION_1";

The second step is to create the action with one of the two methods above. One method creates an action with a payload while the second one creates an action without a payload. Something interesting is that it is more convenient to gather all your action into a constant. This way, later, we will leverage the "ReturnType" to get all action type from the action creator function. For example, the example we are building, the action returns a unique type that uses the string literal of the action's constant.

1export const SharedActions = {
2 action1: createActionPayload<typeof ACTION_1, string>(ACTION_1),

The third step is to consume the action. We can use a reducer and type the action it receives to have the only action from one or many constant we build. You can still restrict which actions a reducer can receive by specifying which variable that holds many actions with "ActionsUnion". In the example below, I mention that any action from the SharedAction object, and the OtherGroupAction and a specific other one is tolerated in that reducer. The greatest feature of this approach is that once you use a conditional statement like an IF or a SWITCH against the action's type, the type of the payload is narrowed down to the action. This gives an excellent IntelliSense.

1type ReducerAcceptedActions = ActionsUnion<typeof SharedActions & typeof OtherGroupOfAction & GroupB.individualAction>;
3export function oneReducerHere( state: State = initialState(), action: ReducerAcceptedActions): State {
4 switch (action.type) {
5 case ACTION_1: { // ... }
6 }

This way to proceed with Redux and TypeScript removes many boilerplate codes that were initially needed in TypeScript. In the end, the code is easy to read even if you have hundreds or thousands of actions because you can separate them in a bundle. Also, the automatic type narrowing is a bless giving a huge edge when developing by bringing a natural boundary of what is available in each payload automatically.