Install typed-redux-saga
masterYou can install typed-redux-saga using either yarn or npm. This package requires TypeScript 3.6 or later.
# yarn
yarn add typed-redux-saga
# npm
npm install typed-redux-sagarepository·master·Indexed 43 days ago
https://github.com/agiledigital/typed-redux-sagaRedux-Saga effects with strong types for TypeScript 3.6+. It provides typed versions of standard redux-saga/effects (such as call, all, put, and take), allowing developers to receive correctly typed results by using yield* instead of yield. Includes a Babel macro to transform typed effects into raw redux-saga effects at compile time to avoid runtime overhead, and an ESLint plugin to prevent accidental imports from the original library.
You can install typed-redux-saga using either yarn or npm. This package requires TypeScript 3.6 or later.
# yarn
yarn add typed-redux-saga
# npm
npm install typed-redux-sagaTo avoid the runtime overhead of typed-redux-saga proxies, you can use the built-in Babel macro. This transforms your typed effects into raw redux-saga effects at compile time.
babel-plugin-macros:yarn add --dev babel-plugin-macrostyped-redux-saga/macro instead of the main entry point.Example:
import {call, race} from "typed-redux-saga/macro";
function* myEffect() {
yield* call(() => "foo");
}This will be transpiled to standard redux-saga code during build.
import {call, race} from "typed-redux-saga/macro";
// And use the library normally
function* myEffect() {
yield* call(() => "foo");
}To get strong types for your saga effects, follow these two steps:
call, all, race, etc.) from typed-redux-saga instead of redux-saga/effects.yield with yield* when calling these effects.This ensures that the result of the effect has the correct type (e.g., the return type of the function being called) instead of any.
import { call, all } from "typed-redux-saga";
import Api from "...";
function* fetchUser(action) {
// `user` now has type User, not any!
const user = yield* call(Api.fetchUser, action.payload.userId);
...
}
function* fetchConfig() {
// `result` now has type {api1: Config1, api2: Config2}
const result = yield* all({
api1: call(Api.fetchConfig1),
api2: call(Api.fetchConfig2),
});
...
}redux-saga/effects (which would break the typing), use the eslint-plugin-typed-redux-saga plugin. It includes an auto-fix option to help convert existing codebases from redux-saga to typed-redux-saga.The typed-redux-saga package provides typed versions of standard redux-saga/effects. These functions are generator functions that wrap the original effects, allowing for better type safety when using TypeScript in your sagas.
Instead of importing effects directly from redux-saga/effects, import them from typed-redux-saga to ensure that action types, payloads, and return values are correctly typed within your generator functions.
The following effects are exported from typed-redux-saga as generator functions, wrapping their redux-saga/effects counterparts:
take / takeMaybe / takeEvery / takeLatest / takeLeading (Action listening)put / putResolve (Action dispatching)call / apply / cps (Function execution)fork / spawn / join / cancel (Task management)select (State selection)actionChannel / flush (Channel management)cancelled (Cancellation detection)setContext / getContext (Context management)delay / throttle / debounce / retry (Timing and flow control)all / race (Combinators)take family of effects allows you to listen for specific actions in a saga. Using the typed versions ensures that the yielded action matches the expected type.call and apply effects are used to execute functions (usually asynchronous ones). The typed versions help ensure the arguments passed and the values returned by the called function are correctly typed.fork and spawn effects are used to run tasks in the background. These typed versions provide safety when managing concurrent sagas.put family of effects allows you to dispatch actions to the Redux store. The typed version ensures the action being dispatched adheres to your defined action types.