TypeScript FSA Reducers provides a fluent, typesafe API for defining Redux reducers. It builds on `typescript-fsa` to streamline reducer creation by removing common boilerplate such as `if-else` chains for action type checking and manual extraction of payloads from actions. Instead, it offers a method-chaining approach with dedicated handlers for specific action creators. The current stable version is 1.2.2. While there isn't a strictly defined release cadence, the library has demonstrated consistent updates and a commitment to stability, notably reaching version 1.0.0. Its key differentiators include the fluent builder pattern (`.case()`, `.default()`, `.withHandling()`, `.build()`), strong type inference for state and payloads, and direct integration with `typescript-fsa`'s action creators, making reducer logic concise and highly maintainable in TypeScript Redux applications.
npm install typescript-fsa-reducersVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining a typesafe Redux reducer using `reducerWithInitialState`, handling different action types with `.case()`, adding a default handler, and calling `.build()` to finalize the reducer. It also shows basic reducer usage.
Ensure `typescript-fsa` is explicitly listed and installed in your project's `dependencies` or `devDependencies`: `npm install typescript-fsa` or `yarn add typescript-fsa`.
Always terminate your reducer chain with `.build()`. For example, `const reducer = reducerWithInitialState(...).case(...).build();`.
When using `reducerWithoutInitialState`, ensure that the first argument passed to the generated reducer is a valid state value or explicitly type your state to include `undefined` if that is intended (though `reducerWithInitialState` is typically preferred for explicit initial states). The library expects `undefined` to trigger initial state logic only for `reducerWithInitialState`.
Be aware that reducers correctly handle `undefined` as a state argument (especially for `reducerWithInitialState`) to trigger initial state usage. Ensure your code accounts for this and doesn't rely on previous type errors for `undefined` state handling.
For complex type interactions, ensure explicit type annotations for state, use `upcastingReducer()` as needed, or review the `typescript-fsa` and Redux documentation on reducer composition and type inference for best practices.
Always call `.build()` at the end of your reducer chain to get the actual reducer function: `const myReducer = reducerWithInitialState(...).case(...).build();`
Install `typescript-fsa` explicitly: `npm install typescript-fsa` or `yarn add typescript-fsa`.
If you intend for `undefined` to trigger an initial state, use `reducerWithInitialState(INITIAL_STATE)`. If you truly want to handle `undefined` as a valid state, ensure your `State` type explicitly includes `| undefined`.