1 | import type { Reducer } from 'redux';
|
2 | import type { ActionCreatorWithoutPayload, PayloadAction, PayloadActionCreator, PrepareAction, _ActionCreatorWithPreparedPayload } from './createAction';
|
3 | import type { CaseReducer, CaseReducers } from './createReducer';
|
4 | import type { ActionReducerMapBuilder } from './mapBuilders';
|
5 | import type { NoInfer } from './tsHelpers';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export declare type SliceActionCreator<P> = PayloadActionCreator<P>;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export interface Slice<State = any, CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> {
|
20 | |
21 |
|
22 |
|
23 | name: Name;
|
24 | |
25 |
|
26 |
|
27 | reducer: Reducer<State>;
|
28 | |
29 |
|
30 |
|
31 |
|
32 | actions: CaseReducerActions<CaseReducers, Name>;
|
33 | |
34 |
|
35 |
|
36 |
|
37 | caseReducers: SliceDefinedCaseReducers<CaseReducers>;
|
38 | |
39 |
|
40 |
|
41 |
|
42 | getInitialState: () => State;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export interface CreateSliceOptions<State = any, CR extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> {
|
50 | |
51 |
|
52 |
|
53 | name: Name;
|
54 | |
55 |
|
56 |
|
57 | initialState: State | (() => State);
|
58 | /**
|
59 | * A mapping from action types to action-type-specific *case reducer*
|
60 | * functions. For every action type, a matching action creator will be
|
61 | * generated using `createAction()`.
|
62 | */
|
63 | reducers: ValidateSliceCaseReducers<State, CR>;
|
64 | /**
|
65 | * A callback that receives a *builder* object to define
|
66 | * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
|
67 | *
|
68 | * Alternatively, a mapping from action types to action-type-specific *case reducer*
|
69 | * functions. These reducers should have existing action types used
|
70 | * as the keys, and action creators will _not_ be generated.
|
71 | *
|
72 | * @example
|
73 | ```ts
|
74 | import { createAction, createSlice, Action, AnyAction } from '@reduxjs/toolkit'
|
75 | const incrementBy = createAction<number>('incrementBy')
|
76 | const decrement = createAction('decrement')
|
77 |
|
78 | interface RejectedAction extends Action {
|
79 | error: Error
|
80 | }
|
81 |
|
82 | function isRejectedAction(action: AnyAction): action is RejectedAction {
|
83 | return action.type.endsWith('rejected')
|
84 | }
|
85 |
|
86 | createSlice({
|
87 | name: 'counter',
|
88 | initialState: 0,
|
89 | reducers: {},
|
90 | extraReducers: builder => {
|
91 | builder
|
92 | .addCase(incrementBy, (state, action) => {
|
93 |
|
94 | })
|
95 |
|
96 | .addCase(decrement, (state, action) => {})
|
97 |
|
98 | .addMatcher(
|
99 | isRejectedAction,
|
100 |
|
101 | (state, action) => {}
|
102 | )
|
103 |
|
104 | .addDefaultCase((state, action) => {})
|
105 | }
|
106 | })
|
107 | ```
|
108 | */
|
109 | extraReducers?: CaseReducers<NoInfer<State>, any> | ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void);
|
110 | }
|
111 | /**
|
112 | * A CaseReducer with a `prepare` method.
|
113 | *
|
114 | * @public
|
115 | */
|
116 | export declare type CaseReducerWithPrepare<State, Action extends PayloadAction> = {
|
117 | reducer: CaseReducer<State, Action>;
|
118 | prepare: PrepareAction<Action['payload']>;
|
119 | };
|
120 | /**
|
121 | * The type describing a slice's `reducers` option.
|
122 | *
|
123 | * @public
|
124 | */
|
125 | export declare type SliceCaseReducers<State> = {
|
126 | [K: string]: CaseReducer<State, PayloadAction<any>> | CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>;
|
127 | };
|
128 | declare type SliceActionType<SliceName extends string, ActionName extends keyof any> = ActionName extends string | number ? `${SliceName}/${ActionName}` : string;
|
129 | /**
|
130 | * Derives the slice's `actions` property from the `reducers` options
|
131 | *
|
132 | * @public
|
133 | */
|
134 | export declare type CaseReducerActions<CaseReducers extends SliceCaseReducers<any>, SliceName extends string> = {
|
135 | [Type in keyof CaseReducers]: CaseReducers[Type] extends {
|
136 | prepare: any;
|
137 | } ? ActionCreatorForCaseReducerWithPrepare<CaseReducers[Type], SliceActionType<SliceName, Type>> : ActionCreatorForCaseReducer<CaseReducers[Type], SliceActionType<SliceName, Type>>;
|
138 | };
|
139 | /**
|
140 | * Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare`
|
141 | *
|
142 | * @internal
|
143 | */
|
144 | declare type ActionCreatorForCaseReducerWithPrepare<CR extends {
|
145 | prepare: any;
|
146 | }, Type extends string> = _ActionCreatorWithPreparedPayload<CR['prepare'], Type>;
|
147 | /**
|
148 | * Get a `PayloadActionCreator` type for a passed `CaseReducer`
|
149 | *
|
150 | * @internal
|
151 | */
|
152 | declare type ActionCreatorForCaseReducer<CR, Type extends string> = CR extends (state: any, action: infer Action) => any ? Action extends {
|
153 | payload: infer P;
|
154 | } ? PayloadActionCreator<P, Type> : ActionCreatorWithoutPayload<Type> : ActionCreatorWithoutPayload<Type>;
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | declare type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
|
162 | [Type in keyof CaseReducers]: CaseReducers[Type] extends {
|
163 | reducer: infer Reducer;
|
164 | } ? Reducer : CaseReducers[Type];
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | export declare type ValidateSliceCaseReducers<S, ACR extends SliceCaseReducers<S>> = ACR & {
|
179 | [T in keyof ACR]: ACR[T] extends {
|
180 | reducer(s: S, action?: infer A): any;
|
181 | } ? {
|
182 | prepare(...a: never[]): Omit<A, 'type'>;
|
183 | } : {};
|
184 | };
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | export declare function createSlice<State, CaseReducers extends SliceCaseReducers<State>, Name extends string = string>(options: CreateSliceOptions<State, CaseReducers, Name>): Slice<State, CaseReducers, Name>;
|
196 | export {};
|