1import type { Reducer } from 'redux'; 2import type { ActionCreatorWithoutPayload, PayloadAction, PayloadActionCreator, PrepareAction, _ActionCreatorWithPreparedPayload } from './createAction'; 3import type { CaseReducer, CaseReducers } from './createReducer'; 4import type { ActionReducerMapBuilder } from './mapBuilders'; 5import type { NoInfer } from './tsHelpers'; 6/** 7 * An action creator attached to a slice. 8 * 9 * @deprecated please use PayloadActionCreator directly 10 * 11 * @public 12 */ 13export declare type SliceActionCreator<P> = PayloadActionCreator<P>; 14/** 15 * The return value of `createSlice` 16 * 17 * @public 18 */ 19export interface Slice<State = any, CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> { 20 /** 21 * The slice name. 22 */ 23 name: Name; 24 /** 25 * The slice's reducer. 26 */ 27 reducer: Reducer<State>; 28 /** 29 * Action creators for the types of actions that are handled by the slice 30 * reducer. 31 */ 32 actions: CaseReducerActions<CaseReducers>; 33 /** 34 * The individual case reducer functions that were passed in the `reducers` parameter. 35 * This enables reuse and testing if they were defined inline when calling `createSlice`. 36 */ 37 caseReducers: SliceDefinedCaseReducers<CaseReducers>; 38} 39/** 40 * Options for `createSlice()`. 41 * 42 * @public 43 */ 44export interface CreateSliceOptions<State = any, CR extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> { 45 /** 46 * The slice's name. Used to namespace the generated action types. 47 */ 48 name: Name; 49 /** 50 * The initial state to be returned by the slice reducer. 51 */ 52 initialState: State; 53 /** 54 * A mapping from action types to action-type-specific *case reducer* 55 * functions. For every action type, a matching action creator will be 56 * generated using `createAction()`. 57 */ 58 reducers: ValidateSliceCaseReducers<State, CR>; 59 /** 60 * A callback that receives a *builder* object to define 61 * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`. 62 * 63 * Alternatively, a mapping from action types to action-type-specific *case reducer* 64 * functions. These reducers should have existing action types used 65 * as the keys, and action creators will _not_ be generated. 66 * 67 * @example 68 ```ts 69 import { createAction, createSlice, Action, AnyAction } from '@reduxjs/toolkit' 70 const incrementBy = createAction<number>('incrementBy') 71 const decrement = createAction('decrement') 72 73 interface RejectedAction extends Action { 74 error: Error 75 } 76 77 function isRejectedAction(action: AnyAction): action is RejectedAction { 78 return action.type.endsWith('rejected') 79 } 80 81 createSlice({ 82 name: 'counter', 83 initialState: 0, 84 reducers: {}, 85 extraReducers: builder => { 86 builder 87 .addCase(incrementBy, (state, action) => { 88 // action is inferred correctly here if using TS 89 }) 90 // You can chain calls, or have separate `builder.addCase()` lines each time 91 .addCase(decrement, (state, action) => {}) 92 // You can match a range of action types 93 .addMatcher( 94 isRejectedAction, 95 // `action` will be inferred as a RejectedAction due to isRejectedAction being defined as a type guard 96 (state, action) => {} 97 ) 98 // and provide a default case if no other handlers matched 99 .addDefaultCase((state, action) => {}) 100 } 101 }) 102 ``` 103 */ 104 extraReducers?: CaseReducers<NoInfer<State>, any> | ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void); 105} 106/** 107 * A CaseReducer with a `prepare` method. 108 * 109 * @public 110 */ 111export declare type CaseReducerWithPrepare<State, Action extends PayloadAction> = { 112 reducer: CaseReducer<State, Action>; 113 prepare: PrepareAction<Action['payload']>; 114}; 115/** 116 * The type describing a slice's `reducers` option. 117 * 118 * @public 119 */ 120export declare type SliceCaseReducers<State> = { 121 [K: string]: CaseReducer<State, PayloadAction<any>> | CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>; 122}; 123/** 124 * Derives the slice's `actions` property from the `reducers` options 125 * 126 * @public 127 */ 128export declare type CaseReducerActions<CaseReducers extends SliceCaseReducers<any>> = { 129 [Type in keyof CaseReducers]: CaseReducers[Type] extends { 130 prepare: any; 131 } ? ActionCreatorForCaseReducerWithPrepare<CaseReducers[Type]> : ActionCreatorForCaseReducer<CaseReducers[Type]>; 132}; 133/** 134 * Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare` 135 * 136 * @internal 137 */ 138declare type ActionCreatorForCaseReducerWithPrepare<CR extends { 139 prepare: any; 140}> = _ActionCreatorWithPreparedPayload<CR['prepare'], string>; 141/** 142 * Get a `PayloadActionCreator` type for a passed `CaseReducer` 143 * 144 * @internal 145 */ 146declare type ActionCreatorForCaseReducer<CR> = CR extends (state: any, action: infer Action) => any ? Action extends { 147 payload: infer P; 148} ? PayloadActionCreator<P> : ActionCreatorWithoutPayload : ActionCreatorWithoutPayload; 149/** 150 * Extracts the CaseReducers out of a `reducers` object, even if they are 151 * tested into a `CaseReducerWithPrepare`. 152 * 153 * @internal 154 */ 155declare type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = { 156 [Type in keyof CaseReducers]: CaseReducers[Type] extends { 157 reducer: infer Reducer; 158 } ? Reducer : CaseReducers[Type]; 159}; 160/** 161 * Used on a SliceCaseReducers object. 162 * Ensures that if a CaseReducer is a `CaseReducerWithPrepare`, that 163 * the `reducer` and the `prepare` function use the same type of `payload`. 164 * 165 * Might do additional such checks in the future. 166 * 167 * This type is only ever useful if you want to write your own wrapper around 168 * `createSlice`. Please don't use it otherwise! 169 * 170 * @public 171 */ 172export declare type ValidateSliceCaseReducers<S, ACR extends SliceCaseReducers<S>> = ACR & { 173 [T in keyof ACR]: ACR[T] extends { 174 reducer(s: S, action?: infer A): any; 175 } ? { 176 prepare(...a: never[]): Omit<A, 'type'>; 177 } : {}; 178}; 179/** 180 * A function that accepts an initial state, an object full of reducer 181 * functions, and a "slice name", and automatically generates 182 * action creators and action types that correspond to the 183 * reducers and state. 184 * 185 * The `reducer` argument is passed to `createReducer()`. 186 * 187 * @public 188 */ 189export declare function createSlice<State, CaseReducers extends SliceCaseReducers<State>, Name extends string = string>(options: CreateSliceOptions<State, CaseReducers, Name>): Slice<State, CaseReducers, Name>; 190export {}; 191