typesafe-redux
Utilities & helpers for increased type safety in a React + Redux application
Example App
A basic example application (todo list) is provided at examples/todos/ which showcases usage of this library to provide type safety in a React + Redux application.
Creating Actions
This library offers a few different ways of creating actions or action creators, which are outlined below. All methods flow through the actionCreator
function.
Flux Standard Actions
The actionCreator
method can be used to create flux standard action (FSA) factories, using the structure const creator = customActionCreator(type)<PayloadType>();
to define the action type constant and shape of the payload:
; // with an object payload:;dispatchaddTodo; // with a simple string payload:;dispatchdeleteTodo'todo-123';;dispatchcompleteTodo'todo-123'; // or with no payload at all:;dispatchcompleteAll;
Once the action type and payload shape have been defined using actionCreator
those values will have types automatically applied when they're used in reducers, so you only have to type them once!
Custom/Complex Actions
The actionCreator
method also accepts a callback argument for anytime it's necessary for an action creator to take arguments that differ from the payload shape (e.g. because of autogenerated timestamps or other computed values):
;;
Identifying Actions in Reducers
Action identification is typically done through a string comparison using action.type
, but with typesafe-redux
the action creator is capable of determining if it produced a given action via the match
method injected into the prototype of action creators made using this library. You can either use this method directly or use the provided isActionFrom
helper:
;