CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typesafe-actions

Typesafe Action Creators for Redux / Flux Architectures in TypeScript

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Typesafe Actions

Typesafe 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.

Package Information

  • Package Name: typesafe-actions
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install typesafe-actions

Core Imports

import { 
  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";

Basic Usage

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,
  }));

Architecture

Typesafe Actions is built around several key components:

  • Action Creators: Functions for creating type-safe actions (createAction, createAsyncAction, etc.)
  • Action Helpers: Utilities for working with actions (getType, isActionOf, isOfType)
  • Type System: Comprehensive TypeScript type definitions for full type safety
  • Reducer Utilities: Type-safe reducer creation with createReducer
  • Legacy Support: Deprecated API maintained for backward compatibility

Capabilities

Action Creators

Core 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>;

Action Creators

Action Helpers

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;

Action Helpers

Type Helpers

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;
}

Type Helpers

Deprecated API

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.

docs

action-creators.md

action-helpers.md

index.md

type-helpers.md

tile.json