Registry / serialization / typesafe-actions

typesafe-actions

JSON →
library5.1.0jsnpmunverified

Typesafe-actions is a lightweight utility library designed to simplify and enhance type safety in Redux and Flux architectures, particularly within TypeScript projects. Currently at version 5.1.0, it focuses on reducing boilerplate and complexity in defining actions and reducers. The library ships with minimal external dependencies, emphasizing a secure and performant codebase. It provides robust tools for creating fully typesafe action creators (including synchronous and asynchronous actions), and streamlined reducer definitions. Typesafe-actions differentiates itself by offering extensive type-testing to ensure soundness across TypeScript versions and provides optimized builds for various environments (CJS, ESM, UMD). While it offers a powerful approach to Redux type safety, it's worth noting that Redux Toolkit is the officially recommended solution by the Redux team, which also offers strong TypeScript integration.

npm install typesafe-actions
INSTALL
IMPORT
SIG · TYPESAFE-ACTIONS
T
typesafe-actions
serializationjavascriptv5.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createAction
import { createAction } from 'typesafe-actions'
import { createStandardAction } from 'typesafe-actions'
Since v5, `createAction` is the primary action creator. The old `createStandardAction` (renamed to `createAction` in v4.4.2) and the original `createAction` from v4 are now under the `deprecated` export for migration.
createReducer
import { createReducer } from 'typesafe-actions'
Used for creating typesafe reducers with an API similar to Redux Toolkit's builder pattern.
createAsyncAction
import { createAsyncAction } from 'typesafe-actions'
For defining typesafe asynchronous action flows, often used with middleware like `redux-observable` or `redux-saga`.
deprecated
import { deprecated } from 'typesafe-actions'; const { createAction, createStandardAction } = deprecated;
Contains all deprecated v4 action creator functions (`createAction`, `createStandardAction`, `createCustomAction`) for easier migration to v5.
ActionType
import { ActionType } from 'typesafe-actions'
A type helper used to infer a union type of all actions from a given action creator or module, useful for reducer definitions.

Demonstrates the creation of typesafe actions and a reducer using `typesafe-actions` in TypeScript, including how to define state, actions with payloads, and combine them into a reducer that handles different action types.

import { createAction, createReducer, ActionType } from 'typesafe-actions'; // 1. Define Actions const increment = createAction('INCREMENT')<number>(); const decrement = createAction('DECREMENT')<void>(); const reset = createAction('RESET')(); const actions = { increment, decrement, reset }; type RootAction = ActionType<typeof actions>; interface CounterState { count: number; } const initialState: CounterState = { count: 0 }; // 2. Create a Reducer const counterReducer = createReducer<CounterState, RootAction>(initialState) .handleAction(increment, (state, action) => ({ ...state, count: state.count + action.payload })) .handleAction(decrement, (state) => ({ ...state, count: state.count - 1 })) .handleAction(reset, (state) => ({ ...state, count: 0 })); // Example Usage (simulate Redux store) let currentState = initialState; console.log('Initial State:', currentState); currentState = counterReducer(currentState, actions.increment(5)); console.log('After Increment by 5:', currentState); currentState = counterReducer(currentState, actions.decrement()); console.log('After Decrement:', currentState); currentState = counterReducer(currentState, actions.reset()); console.log('After Reset:', currentState);
Debug
Known issues
breakingIn `v5`, all deprecated `v4` action creator functions (`createAction`, `createStandardAction`, `createCustomAction`) have been moved under a named `deprecated` import. Direct imports of these functions from the root are no longer available.
fix
Update imports from `import { createAction } from 'typesafe-actions'` to `import { deprecated } from 'typesafe-actions'; const { createAction } = deprecated;` for v4-style creators. Prefer using the new `createAction` API for new code.
affects: >=5.0.0
breakingIn `v4.4.2`, `createStandardAction` was renamed to `createAction`, and its `.map` method was removed. If upgrading from versions prior to `v4.4.2`, code using `createStandardAction` or its `.map` method will break.
fix
Refactor `createStandardAction` usage to the new `createAction` signature. For `.map` functionality, explicitly define payload and meta creators within the `createAction` call.
affects: >=4.4.2 <5.0.0
gotchaThe official documentation and tutorial for `typesafe-actions` are currently outdated for `v5.x.x` and reflect `v4` APIs. Users are directed to a GitHub issue for temporary `v5.x.x` API documentation.
fix
Refer to the GitHub issue referenced in the README (e.g., #143 or similar) for the most current `v5` API usage and breaking changes information.
affects: >=5.0.0
gotchaThe Redux team officially recommends Redux Toolkit (`@reduxjs/toolkit`) for building Redux applications, which is also fully type-safe and aims to reduce boilerplate. Users considering `typesafe-actions` might want to evaluate Redux Toolkit as an alternative.
fix
Review Redux Toolkit's documentation and compare its features and API with `typesafe-actions` to determine the best fit for your project. Redux Toolkit offers `createSlice` and `createAction` functions with built-in TypeScript support.
affects: *
Errors
Common errors & fixes
TS2305: Module '"typesafe-actions"' has no exported member 'RootAction'.
The `RootAction` type helper was not properly exported or inferred, or there was a change in how `RootAction` should be defined and extended within the module.
fix
Ensure `RootAction` is correctly defined and exported in your application's types (e.g., `export type RootAction = ActionType<typeof import('./actions').default>;`) and extend the `typesafe-actions` module's `Types` interface if using the type-free `createReducer` syntax. This was specifically resolved in `v4.1.2` for some scenarios.
Error: createReducer cannot be called when handling more than 2 actions
An internal limitation or bug in older versions of `createReducer` that prevented it from correctly processing handlers for a large number of actions.
fix
Upgrade `typesafe-actions` to version `v4.1.3` or higher, which includes a fix for this specific issue.
Error: Actions may not have an undefined "type" property.
This error typically occurs when an action creator is called without providing a type string or if the type property is implicitly `undefined`. It could also indicate an incorrect migration path for action creators.
fix
Ensure that all action creators, especially those migrated from older APIs or custom implementations, explicitly define a string `type` property. Review the `v5` API for `createAction` to ensure correct usage.
Upgrade
Version history
5.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources