123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import type { Reducer } from 'redux';
- import type { ActionCreatorWithoutPayload, PayloadAction, PayloadActionCreator, PrepareAction, _ActionCreatorWithPreparedPayload } from './createAction';
- import type { CaseReducer, CaseReducers } from './createReducer';
- import type { ActionReducerMapBuilder } from './mapBuilders';
- import type { NoInfer } from './tsHelpers';
- /**
- * An action creator attached to a slice.
- *
- * @deprecated please use PayloadActionCreator directly
- *
- * @public
- */
- export declare type SliceActionCreator<P> = PayloadActionCreator<P>;
- /**
- * The return value of `createSlice`
- *
- * @public
- */
- export interface Slice<State = any, CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> {
- /**
- * The slice name.
- */
- name: Name;
- /**
- * The slice's reducer.
- */
- reducer: Reducer<State>;
- /**
- * Action creators for the types of actions that are handled by the slice
- * reducer.
- */
- actions: CaseReducerActions<CaseReducers, Name>;
- /**
- * The individual case reducer functions that were passed in the `reducers` parameter.
- * This enables reuse and testing if they were defined inline when calling `createSlice`.
- */
- caseReducers: SliceDefinedCaseReducers<CaseReducers>;
- /**
- * Provides access to the initial state value given to the slice.
- * If a lazy state initializer was provided, it will be called and a fresh value returned.
- */
- getInitialState: () => State;
- }
- /**
- * Options for `createSlice()`.
- *
- * @public
- */
- export interface CreateSliceOptions<State = any, CR extends SliceCaseReducers<State> = SliceCaseReducers<State>, Name extends string = string> {
- /**
- * The slice's name. Used to namespace the generated action types.
- */
- name: Name;
- /**
- * The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
- */
- initialState: State | (() => State);
- /**
- * A mapping from action types to action-type-specific *case reducer*
- * functions. For every action type, a matching action creator will be
- * generated using `createAction()`.
- */
- reducers: ValidateSliceCaseReducers<State, CR>;
- /**
- * A callback that receives a *builder* object to define
- * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
- *
- * Alternatively, a mapping from action types to action-type-specific *case reducer*
- * functions. These reducers should have existing action types used
- * as the keys, and action creators will _not_ be generated.
- *
- * @example
- ```ts
- import { createAction, createSlice, Action, AnyAction } from '@reduxjs/toolkit'
- const incrementBy = createAction<number>('incrementBy')
- const decrement = createAction('decrement')
-
- interface RejectedAction extends Action {
- error: Error
- }
-
- function isRejectedAction(action: AnyAction): action is RejectedAction {
- return action.type.endsWith('rejected')
- }
-
- createSlice({
- name: 'counter',
- initialState: 0,
- reducers: {},
- extraReducers: builder => {
- builder
- .addCase(incrementBy, (state, action) => {
- // action is inferred correctly here if using TS
- })
- // You can chain calls, or have separate `builder.addCase()` lines each time
- .addCase(decrement, (state, action) => {})
- // You can match a range of action types
- .addMatcher(
- isRejectedAction,
- // `action` will be inferred as a RejectedAction due to isRejectedAction being defined as a type guard
- (state, action) => {}
- )
- // and provide a default case if no other handlers matched
- .addDefaultCase((state, action) => {})
- }
- })
- ```
- */
- extraReducers?: CaseReducers<NoInfer<State>, any> | ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void);
- }
- /**
- * A CaseReducer with a `prepare` method.
- *
- * @public
- */
- export declare type CaseReducerWithPrepare<State, Action extends PayloadAction> = {
- reducer: CaseReducer<State, Action>;
- prepare: PrepareAction<Action['payload']>;
- };
- /**
- * The type describing a slice's `reducers` option.
- *
- * @public
- */
- export declare type SliceCaseReducers<State> = {
- [K: string]: CaseReducer<State, PayloadAction<any>> | CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>;
- };
- declare type SliceActionType<SliceName extends string, ActionName extends keyof any> = ActionName extends string | number ? `${SliceName}/${ActionName}` : string;
- /**
- * Derives the slice's `actions` property from the `reducers` options
- *
- * @public
- */
- export declare type CaseReducerActions<CaseReducers extends SliceCaseReducers<any>, SliceName extends string> = {
- [Type in keyof CaseReducers]: CaseReducers[Type] extends {
- prepare: any;
- } ? ActionCreatorForCaseReducerWithPrepare<CaseReducers[Type], SliceActionType<SliceName, Type>> : ActionCreatorForCaseReducer<CaseReducers[Type], SliceActionType<SliceName, Type>>;
- };
- /**
- * Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare`
- *
- * @internal
- */
- declare type ActionCreatorForCaseReducerWithPrepare<CR extends {
- prepare: any;
- }, Type extends string> = _ActionCreatorWithPreparedPayload<CR['prepare'], Type>;
- /**
- * Get a `PayloadActionCreator` type for a passed `CaseReducer`
- *
- * @internal
- */
- declare type ActionCreatorForCaseReducer<CR, Type extends string> = CR extends (state: any, action: infer Action) => any ? Action extends {
- payload: infer P;
- } ? PayloadActionCreator<P, Type> : ActionCreatorWithoutPayload<Type> : ActionCreatorWithoutPayload<Type>;
- /**
- * Extracts the CaseReducers out of a `reducers` object, even if they are
- * tested into a `CaseReducerWithPrepare`.
- *
- * @internal
- */
- declare type SliceDefinedCaseReducers<CaseReducers extends SliceCaseReducers<any>> = {
- [Type in keyof CaseReducers]: CaseReducers[Type] extends {
- reducer: infer Reducer;
- } ? Reducer : CaseReducers[Type];
- };
- /**
- * Used on a SliceCaseReducers object.
- * Ensures that if a CaseReducer is a `CaseReducerWithPrepare`, that
- * the `reducer` and the `prepare` function use the same type of `payload`.
- *
- * Might do additional such checks in the future.
- *
- * This type is only ever useful if you want to write your own wrapper around
- * `createSlice`. Please don't use it otherwise!
- *
- * @public
- */
- export declare type ValidateSliceCaseReducers<S, ACR extends SliceCaseReducers<S>> = ACR & {
- [T in keyof ACR]: ACR[T] extends {
- reducer(s: S, action?: infer A): any;
- } ? {
- prepare(...a: never[]): Omit<A, 'type'>;
- } : {};
- };
- /**
- * A function that accepts an initial state, an object full of reducer
- * functions, and a "slice name", and automatically generates
- * action creators and action types that correspond to the
- * reducers and state.
- *
- * The `reducer` argument is passed to `createReducer()`.
- *
- * @public
- */
- export declare function createSlice<State, CaseReducers extends SliceCaseReducers<State>, Name extends string = string>(options: CreateSliceOptions<State, CaseReducers, Name>): Slice<State, CaseReducers, Name>;
- export {};
|