Typesafe Action Creators for Redux / Flux Architectures in TypeScript
npx @tessl/cli install tessl/npm-typesafe-actions@5.1.0Typesafe Actions is a TypeScript library that provides typesafe utilities designed to reduce types verbosity and complexity in Redux/Flux architectures. It offers a comprehensive set of action creators, action helpers, type helpers, and reducer utilities with full type safety and enhanced developer experience.
npm install typesafe-actionsimport {
action,
createAction,
createCustomAction,
createAsyncAction,
createReducer,
getType,
isActionOf,
isOfType,
deprecated
} from "typesafe-actions";For CommonJS:
const {
action,
createAction,
createCustomAction,
createAsyncAction,
createReducer,
getType,
isActionOf,
isOfType,
deprecated
} = require("typesafe-actions");For TypeScript types:
import type {
Action,
ActionCreator,
ActionType,
StateType,
EmptyAction,
PayloadAction,
PayloadMetaAction,
ActionCreatorBuilder,
AsyncActionCreatorBuilder
} from "typesafe-actions";import { createAction, createAsyncAction, createReducer } from "typesafe-actions";
// Create simple action
const increment = createAction('INCREMENT')<number>();
const decrement = createAction('DECREMENT')();
// Create async action
const fetchUser = createAsyncAction(
'FETCH_USER_REQUEST',
'FETCH_USER_SUCCESS',
'FETCH_USER_FAILURE'
)<void, User, Error>();
// Create reducer
const counterReducer = createReducer({ count: 0 })
.handleAction(increment, (state, action) => ({
count: state.count + action.payload,
}))
.handleAction(decrement, (state) => ({
count: state.count - 1,
}));Typesafe Actions is built around several key components:
createAction, createAsyncAction, etc.)getType, isActionOf, isOfType)createReducerCore action creation functionality providing type-safe action creators with payload and meta support, async action patterns, and reducer utilities.
function action<T extends TypeConstant>(type: T): { type: T };
function createAction<TType extends string>(
type: TType
): ActionCreatorBuilder<TType>;
function createCustomAction<
TType extends TypeConstant,
TArgs extends any[] = [],
TReturn extends any = {}
>(
type: TType,
createHandler?: (...args: TArgs) => TReturn
): ((...args: TArgs) => ResolveType<{ type: TType } & TReturn>) &
ActionCreatorTypeMetadata<TType>;
function createAsyncAction<
TRequestType extends string,
TSuccessType extends string,
TFailureType extends string
>(
requestType: TRequestType,
successType: TSuccessType,
failureType: TFailureType
): AsyncActionCreatorBuilder<TRequestType, TSuccessType, TFailureType>;
function createReducer<TState, TRootAction extends Action = Types["RootAction"]>(
initialState: TState
): ReducerBuilder<TState, TRootAction>;Utilities for type-safe action inspection and filtering, including type guards and action creator type extraction.
function getType<TType extends TypeConstant>(
actionCreator: ActionCreator<TType> & ActionCreatorTypeMetadata<TType>
): TType;
function isActionOf<AC extends ActionCreator<{ type: string }>>(
actionCreator: AC | AC[]
): (action: { type: string }) => action is ReturnType<AC>;
function isOfType<T extends string>(
type: T | T[]
): <A extends { type: string }>(action: A) => action is A extends { type: T } ? A : never;Complete TypeScript type definitions and interfaces for building type-safe Redux applications with action creators, reducers, and state management.
type TypeConstant = string;
interface Action<TType extends TypeConstant = TypeConstant> {
type: TType;
}
type ActionCreator<TAction extends Action = Action> = (
...args: any[]
) => TAction;
interface ActionCreatorTypeMetadata<TType extends TypeConstant> {
getType?(): TType;
}
type ActionType<TActionCreatorOrMap extends any> =
TActionCreatorOrMap extends ActionCreator<infer TAction>
? TAction
: TActionCreatorOrMap extends Record<any, any>
? ActionType<TActionCreatorOrMap[keyof TActionCreatorOrMap]>
: never;
type ResolveType<T> = T extends (...args: any[]) => any ? ReturnType<T> : T;
interface Types {
RootAction: Action;
}Backward-compatible API for migrating from previous versions (v4.x.x). Available under the deprecated named import.
import { deprecated } from "typesafe-actions";
const { createAction, createStandardAction, createCustomAction } = deprecated;The deprecated API includes legacy versions of action creators that were available in v4.x.x and earlier versions for incremental migration support.