UNPKG

5 kBTypeScriptView Raw
1import type { Action, AnyAction } from 'redux';
2import type { CaseReducer, CaseReducers, ActionMatcher, ActionMatcherDescriptionCollection } from './createReducer';
3export interface TypedActionCreator<Type extends string> {
4 (...args: any[]): Action<Type>;
5 type: Type;
6}
7/**
8 * A builder for an action <-> reducer map.
9 *
10 * @public
11 */
12export interface ActionReducerMapBuilder<State> {
13 /**
14 * Adds a case reducer to handle a single exact action type.
15 * @remarks
16 * All calls to `builder.addCase` must come before any calls to `builder.addMatcher` or `builder.addDefaultCase`.
17 * @param actionCreator - Either a plain action type string, or an action creator generated by [`createAction`](./createAction) that can be used to determine the action type.
18 * @param reducer - The actual case reducer function.
19 */
20 addCase<ActionCreator extends TypedActionCreator<string>>(actionCreator: ActionCreator, reducer: CaseReducer<State, ReturnType<ActionCreator>>): ActionReducerMapBuilder<State>;
21 /**
22 * Adds a case reducer to handle a single exact action type.
23 * @remarks
24 * All calls to `builder.addCase` must come before any calls to `builder.addMatcher` or `builder.addDefaultCase`.
25 * @param actionCreator - Either a plain action type string, or an action creator generated by [`createAction`](./createAction) that can be used to determine the action type.
26 * @param reducer - The actual case reducer function.
27 */
28 addCase<Type extends string, A extends Action<Type>>(type: Type, reducer: CaseReducer<State, A>): ActionReducerMapBuilder<State>;
29 /**
30 * Allows you to match your incoming actions against your own filter function instead of only the `action.type` property.
31 * @remarks
32 * If multiple matcher reducers match, all of them will be executed in the order
33 * they were defined in - even if a case reducer already matched.
34 * All calls to `builder.addMatcher` must come after any calls to `builder.addCase` and before any calls to `builder.addDefaultCase`.
35 * @param matcher - A matcher function. In TypeScript, this should be a [type predicate](https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates)
36 * function
37 * @param reducer - The actual case reducer function.
38 *
39 * @example
40 ```ts
41 import {
42 createAction,
43 createReducer,
44 AsyncThunk,
45 AnyAction,
46 } from "@reduxjs/toolkit";
47
48 type GenericAsyncThunk = AsyncThunk<unknown, unknown, any>;
49
50 type PendingAction = ReturnType<GenericAsyncThunk["pending"]>;
51 type RejectedAction = ReturnType<GenericAsyncThunk["rejected"]>;
52 type FulfilledAction = ReturnType<GenericAsyncThunk["fulfilled"]>;
53
54 const initialState: Record<string, string> = {};
55 const resetAction = createAction("reset-tracked-loading-state");
56
57 function isPendingAction(action: AnyAction): action is PendingAction {
58 return action.type.endsWith("/pending");
59 }
60
61 const reducer = createReducer(initialState, (builder) => {
62 builder
63 .addCase(resetAction, () => initialState)
64 // matcher can be defined outside as a type predicate function
65 .addMatcher(isPendingAction, (state, action) => {
66 state[action.meta.requestId] = "pending";
67 })
68 .addMatcher(
69 // matcher can be defined inline as a type predicate function
70 (action): action is RejectedAction => action.type.endsWith("/rejected"),
71 (state, action) => {
72 state[action.meta.requestId] = "rejected";
73 }
74 )
75 // matcher can just return boolean and the matcher can receive a generic argument
76 .addMatcher<FulfilledAction>(
77 (action) => action.type.endsWith("/fulfilled"),
78 (state, action) => {
79 state[action.meta.requestId] = "fulfilled";
80 }
81 );
82 });
83 ```
84 */
85 addMatcher<A extends AnyAction>(matcher: ActionMatcher<A> | ((action: AnyAction) => boolean), reducer: CaseReducer<State, A>): Omit<ActionReducerMapBuilder<State>, 'addCase'>;
86 /**
87 * Adds a "default case" reducer that is executed if no case reducer and no matcher
88 * reducer was executed for this action.
89 * @param reducer - The fallback "default case" reducer function.
90 *
91 * @example
92 ```ts
93 import { createReducer } from '@reduxjs/toolkit'
94 const initialState = { otherActions: 0 }
95 const reducer = createReducer(initialState, builder => {
96 builder
97 // .addCase(...)
98 // .addMatcher(...)
99 .addDefaultCase((state, action) => {
100 state.otherActions++
101 })
102 })
103 ```
104 */
105 addDefaultCase(reducer: CaseReducer<State, AnyAction>): {};
106}
107export declare function executeReducerBuilderCallback<S>(builderCallback: (builder: ActionReducerMapBuilder<S>) => void): [
108 CaseReducers<S, any>,
109 ActionMatcherDescriptionCollection<S>,
110 CaseReducer<S, AnyAction> | undefined
111];
112
\No newline at end of file