import type { Action, UnknownAction, Reducer } from 'redux'; import type { Selector } from 'reselect'; import type { ActionCreatorWithoutPayload, PayloadAction, PayloadActionCreator, PrepareAction, _ActionCreatorWithPreparedPayload } from './createAction'; import type { CaseReducer } from './createReducer'; import type { ActionReducerMapBuilder } from './mapBuilders'; import type { Id } from './tsHelpers'; import type { InjectConfig } from './combineSlices'; import type { AsyncThunk, AsyncThunkConfig, AsyncThunkOptions, AsyncThunkPayloadCreator, OverrideThunkApiConfigs } from './createAsyncThunk'; import { createAsyncThunk as _createAsyncThunk } from './createAsyncThunk'; declare const asyncThunkSymbol: unique symbol; export declare const asyncThunkCreator: { [asyncThunkSymbol]: typeof _createAsyncThunk; }; interface InjectIntoConfig extends InjectConfig { reducerPath?: NewReducerPath; } /** * The return value of `createSlice` * * @public */ export interface Slice = SliceCaseReducers, Name extends string = string, ReducerPath extends string = Name, Selectors extends SliceSelectors = SliceSelectors> { /** * The slice name. */ name: Name; /** * The slice reducer path. */ reducerPath: ReducerPath; /** * The slice's reducer. */ reducer: Reducer; /** * Action creators for the types of actions that are handled by the slice * reducer. */ actions: CaseReducerActions; /** * 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; /** * 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; /** * Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter) */ getSelectors(): Id>; /** * Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state) */ getSelectors(selectState: (rootState: RootState) => State): Id>; /** * Selectors that assume the slice's state is `rootState[slice.reducerPath]` (which is usually the case) * * Equivalent to `slice.getSelectors((state: RootState) => state[slice.reducerPath])`. */ get selectors(): Id>; /** * Inject slice into provided reducer (return value from `combineSlices`), and return injected slice. */ injectInto(this: this, injectable: { inject: (slice: { reducerPath: string; reducer: Reducer; }, config?: InjectConfig) => void; }, config?: InjectIntoConfig): InjectedSlice; /** * Select the slice state, using the slice's current reducerPath. * * Will throw an error if slice is not found. */ selectSlice(state: { [K in ReducerPath]: State; }): State; } /** * A slice after being called with `injectInto(reducer)`. * * Selectors can now be called with an `undefined` value, in which case they use the slice's initial state. */ interface InjectedSlice = SliceCaseReducers, Name extends string = string, ReducerPath extends string = Name, Selectors extends SliceSelectors = SliceSelectors> extends Omit, 'getSelectors' | 'selectors'> { /** * Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter) */ getSelectors(): Id>; /** * Get globalised slice selectors (`selectState` callback is expected to receive first parameter and return slice state) */ getSelectors(selectState: (rootState: RootState) => State | undefined): Id>; /** * Selectors that assume the slice's state is `rootState[slice.name]` (which is usually the case) * * Equivalent to `slice.getSelectors((state: RootState) => state[slice.name])`. */ get selectors(): Id>; /** * Select the slice state, using the slice's current reducerPath. * * Returns initial state if slice is not found. */ selectSlice(state: { [K in ReducerPath]?: State | undefined; }): State; } /** * Options for `createSlice()`. * * @public */ export interface CreateSliceOptions = SliceCaseReducers, Name extends string = string, ReducerPath extends string = Name, Selectors extends SliceSelectors = SliceSelectors> { /** * The slice's name. Used to namespace the generated action types. */ name: Name; /** * The slice's reducer path. Used when injecting into a combined slice reducer. */ reducerPath?: ReducerPath; /** * 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 | ((creators: ReducerCreators) => CR); /** * A callback that receives a *builder* object to define * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`. * * * @example ```ts import { createAction, createSlice, Action } from '@reduxjs/toolkit' const incrementBy = createAction('incrementBy') const decrement = createAction('decrement') interface RejectedAction extends Action { error: Error } function isRejectedAction(action: Action): 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?: (builder: ActionReducerMapBuilder) => void; /** * A map of selectors that receive the slice's state and any additional arguments, and return a result. */ selectors?: Selectors; } export declare enum ReducerType { reducer = "reducer", reducerWithPrepare = "reducerWithPrepare", asyncThunk = "asyncThunk" } interface ReducerDefinition { _reducerDefinitionType: T; } export interface CaseReducerDefinition extends CaseReducer, ReducerDefinition { } /** * A CaseReducer with a `prepare` method. * * @public */ export type CaseReducerWithPrepare = { reducer: CaseReducer; prepare: PrepareAction; }; export interface CaseReducerWithPrepareDefinition extends CaseReducerWithPrepare, ReducerDefinition { } export interface AsyncThunkSliceReducerConfig { pending?: CaseReducer['pending']>>; rejected?: CaseReducer['rejected']>>; fulfilled?: CaseReducer['fulfilled']>>; settled?: CaseReducer['rejected' | 'fulfilled']>>; options?: AsyncThunkOptions; } export interface AsyncThunkSliceReducerDefinition extends AsyncThunkSliceReducerConfig, ReducerDefinition { payloadCreator: AsyncThunkPayloadCreator; } /** * Providing these as part of the config would cause circular types, so we disallow passing them */ type PreventCircular = { [K in keyof ThunkApiConfig]: K extends 'state' | 'dispatch' ? never : ThunkApiConfig[K]; }; interface AsyncThunkCreator = PreventCircular> { (payloadCreator: AsyncThunkPayloadCreator, config?: AsyncThunkSliceReducerConfig): AsyncThunkSliceReducerDefinition; = {}>(payloadCreator: AsyncThunkPayloadCreator, config?: AsyncThunkSliceReducerConfig): AsyncThunkSliceReducerDefinition; withTypes>(): AsyncThunkCreator>; } export interface ReducerCreators { reducer(caseReducer: CaseReducer): CaseReducerDefinition; reducer(caseReducer: CaseReducer>): CaseReducerDefinition>; asyncThunk: AsyncThunkCreator; preparedReducer>(prepare: Prepare, reducer: CaseReducer>>): { _reducerDefinitionType: ReducerType.reducerWithPrepare; prepare: Prepare; reducer: CaseReducer>>; }; } /** * The type describing a slice's `reducers` option. * * @public */ export type SliceCaseReducers = Record> | CaseReducerWithPrepareDefinition> | AsyncThunkSliceReducerDefinition> | Record> | CaseReducerWithPrepare>>; /** * The type describing a slice's `selectors` option. */ export type SliceSelectors = { [K: string]: (sliceState: State, ...args: any[]) => any; }; type SliceActionType = ActionName extends string | number ? `${SliceName}/${ActionName}` : string; /** * Derives the slice's `actions` property from the `reducers` options * * @public */ export type CaseReducerActions, SliceName extends string> = { [Type in keyof CaseReducers]: CaseReducers[Type] extends infer Definition ? Definition extends { prepare: any; } ? ActionCreatorForCaseReducerWithPrepare> : Definition extends AsyncThunkSliceReducerDefinition ? AsyncThunk : Definition extends { reducer: any; } ? ActionCreatorForCaseReducer> : ActionCreatorForCaseReducer> : never; }; /** * Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare` * * @internal */ type ActionCreatorForCaseReducerWithPrepare = _ActionCreatorWithPreparedPayload; /** * Get a `PayloadActionCreator` type for a passed `CaseReducer` * * @internal */ type ActionCreatorForCaseReducer = CR extends (state: any, action: infer Action) => any ? Action extends { payload: infer P; } ? PayloadActionCreator : ActionCreatorWithoutPayload : ActionCreatorWithoutPayload; /** * Extracts the CaseReducers out of a `reducers` object, even if they are * tested into a `CaseReducerWithPrepare`. * * @internal */ type SliceDefinedCaseReducers> = { [Type in keyof CaseReducers]: CaseReducers[Type] extends infer Definition ? Definition extends AsyncThunkSliceReducerDefinition ? Id, 'fulfilled' | 'rejected' | 'pending' | 'settled'>> : Definition extends { reducer: infer Reducer; } ? Reducer : Definition : never; }; type RemappedSelector = S extends Selector ? Selector & { unwrapped: S; } : never; /** * Extracts the final selector type from the `selectors` object. * * Removes the `string` index signature from the default value. */ type SliceDefinedSelectors, RootState> = { [K in keyof Selectors as string extends K ? never : K]: RemappedSelector; }; /** * 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 type ValidateSliceCaseReducers> = ACR & { [T in keyof ACR]: ACR[T] extends { reducer(s: S, action?: infer A): any; } ? { prepare(...a: never[]): Omit; } : {}; }; interface BuildCreateSliceConfig { creators?: { asyncThunk?: typeof asyncThunkCreator; }; } export declare function buildCreateSlice({ creators }?: BuildCreateSliceConfig): , Name extends string, Selectors extends SliceSelectors, ReducerPath extends string = Name>(options: CreateSliceOptions) => Slice; /** * 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. * * @public */ export declare const createSlice: , Name extends string, Selectors extends SliceSelectors, ReducerPath extends string = Name>(options: CreateSliceOptions) => Slice; export {};