{"id":15250,"library":"typesafe-actions","title":"Typesafe Action Creators for Redux","description":"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.","status":"active","version":"5.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/piotrwitek/typesafe-actions","tags":["javascript","typescript","typesafe","actions","action-creator","redux","flux","redux-actions","static-typing"],"install":[{"cmd":"npm install typesafe-actions","lang":"bash","label":"npm"},{"cmd":"yarn add typesafe-actions","lang":"bash","label":"yarn"},{"cmd":"pnpm add typesafe-actions","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { createStandardAction } from 'typesafe-actions'","symbol":"createAction","correct":"import { createAction } from 'typesafe-actions'"},{"note":"Used for creating typesafe reducers with an API similar to Redux Toolkit's builder pattern.","symbol":"createReducer","correct":"import { createReducer } from 'typesafe-actions'"},{"note":"For defining typesafe asynchronous action flows, often used with middleware like `redux-observable` or `redux-saga`.","symbol":"createAsyncAction","correct":"import { createAsyncAction } from 'typesafe-actions'"},{"note":"Contains all deprecated v4 action creator functions (`createAction`, `createStandardAction`, `createCustomAction`) for easier migration to v5.","symbol":"deprecated","correct":"import { deprecated } from 'typesafe-actions'; const { createAction, createStandardAction } = deprecated;"},{"note":"A type helper used to infer a union type of all actions from a given action creator or module, useful for reducer definitions.","symbol":"ActionType","correct":"import { ActionType } from 'typesafe-actions'"}],"quickstart":{"code":"import { createAction, createReducer, ActionType } from 'typesafe-actions';\n\n// 1. Define Actions\nconst increment = createAction('INCREMENT')<number>();\nconst decrement = createAction('DECREMENT')<void>();\nconst reset = createAction('RESET')();\n\nconst actions = {\n  increment,\n  decrement,\n  reset\n};\n\ntype RootAction = ActionType<typeof actions>;\n\ninterface CounterState {\n  count: number;\n}\n\nconst initialState: CounterState = {\n  count: 0\n};\n\n// 2. Create a Reducer\nconst counterReducer = createReducer<CounterState, RootAction>(initialState)\n  .handleAction(increment, (state, action) => ({\n    ...state,\n    count: state.count + action.payload\n  }))\n  .handleAction(decrement, (state) => ({\n    ...state,\n    count: state.count - 1\n  }))\n  .handleAction(reset, (state) => ({\n    ...state,\n    count: 0\n  }));\n\n// Example Usage (simulate Redux store)\nlet currentState = initialState;\n\nconsole.log('Initial State:', currentState);\n\ncurrentState = counterReducer(currentState, actions.increment(5));\nconsole.log('After Increment by 5:', currentState);\n\ncurrentState = counterReducer(currentState, actions.decrement());\nconsole.log('After Decrement:', currentState);\n\ncurrentState = counterReducer(currentState, actions.reset());\nconsole.log('After Reset:', currentState);","lang":"typescript","description":"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."},"warnings":[{"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.","message":"In `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.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Refactor `createStandardAction` usage to the new `createAction` signature. For `.map` functionality, explicitly define payload and meta creators within the `createAction` call.","message":"In `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.","severity":"breaking","affected_versions":">=4.4.2 <5.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=5.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"TS2305: Module '\"typesafe-actions\"' has no exported member 'RootAction'."},{"fix":"Upgrade `typesafe-actions` to version `v4.1.3` or higher, which includes a fix for this specific issue.","cause":"An internal limitation or bug in older versions of `createReducer` that prevented it from correctly processing handlers for a large number of actions.","error":"Error: createReducer cannot be called when handling more than 2 actions"},{"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.","cause":"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.","error":"Error: Actions may not have an undefined \"type\" property."}],"ecosystem":"npm"}