UNPKG

@evertbouw/redux-toolkit

Version:
718 lines (656 loc) 26.5 kB
import { Action } from 'redux'; import { AnyAction } from 'redux'; import { createSelector } from 'reselect'; import { DeepPartial } from 'redux'; import { Dispatch } from 'redux'; import { EnhancerOptions } from 'redux-devtools-extension'; import { Middleware } from 'redux'; import { Reducer } from 'redux'; import { ReducersMapObject } from 'redux'; import { Store } from 'redux'; import { StoreEnhancer } from 'redux'; import { ThunkAction } from 'redux-thunk'; import { ThunkMiddleware } from 'redux-thunk'; /** * Get a `PayloadActionCreator` type for a passed `CaseReducer` * * @internal */ declare type ActionCreatorForCaseReducer<CR> = CR extends (state: any, action: infer Action) => any ? Action extends { payload: infer P; } ? PayloadActionCreator<P> : ActionCreatorWithoutPayload : ActionCreatorWithoutPayload; /** * Get a `PayloadActionCreator` type for a passed `CaseReducerWithPrepare` * * @internal */ declare type ActionCreatorForCaseReducerWithPrepare<CR extends { prepare: any; }> = _ActionCreatorWithPreparedPayload<CR['prepare'], string>; /** * An action creator of type `T` whose `payload` type could not be inferred. Accepts everything as `payload`. * * @inheritdoc {redux#ActionCreator} * * @public */ export declare interface ActionCreatorWithNonInferrablePayload<T extends string = string> extends BaseActionCreator<unknown, T> { /** * Calling this {@link redux#ActionCreator} with an argument will * return a {@link PayloadAction} of type `T` with a payload * of exactly the type of the argument. */ <PT extends unknown>(payload: PT): PayloadAction<PT, T>; } /** * An action creator of type `T` that takes an optional payload of type `P`. * * @inheritdoc {redux#ActionCreator} * * @public */ export declare interface ActionCreatorWithOptionalPayload<P, T extends string = string> extends BaseActionCreator<P, T> { /** * Calling this {@link redux#ActionCreator} without arguments will * return a {@link PayloadAction} of type `T` with a payload of `undefined` */ (payload?: undefined): PayloadAction<undefined, T>; /** * Calling this {@link redux#ActionCreator} with an argument will * return a {@link PayloadAction} of type `T` with a payload of `P` */ <PT extends Diff<P, undefined>>(payload?: PT): PayloadAction<PT, T>; } /** * An action creator of type `T` that takes no payload. * * @inheritdoc {redux#ActionCreator} * * @public */ export declare interface ActionCreatorWithoutPayload<T extends string = string> extends BaseActionCreator<undefined, T> { /** * Calling this {@link redux#ActionCreator} will * return a {@link PayloadAction} of type `T` with a payload of `undefined` */ (): PayloadAction<undefined, T>; } /** * An action creator of type `T` that requires a payload of type P. * * @inheritdoc {redux#ActionCreator} * * @public */ export declare interface ActionCreatorWithPayload<P, T extends string = string> extends BaseActionCreator<P, T> { /** * Calling this {@link redux#ActionCreator} with an argument will * return a {@link PayloadAction} of type `T` with a payload of `P` * If possible, `P` will be narrowed down to the exact type of the payload argument. */ <PT extends P>(payload: PT): PayloadAction<PT, T>; /** * Calling this {@link redux#ActionCreator} with an argument will * return a {@link PayloadAction} of type `T` with a payload of `P` */ (payload: P): PayloadAction<P, T>; } /** * An action creator that takes multiple arguments that are passed * to a `PrepareAction` method to create the final Action. * @typeParam Args arguments for the action creator function * @typeParam P `payload` type * @typeParam T `type` name * @typeParam E optional `error` type * @typeParam M optional `meta` type * * @inheritdoc {redux#ActionCreator} * * @public */ export declare interface ActionCreatorWithPreparedPayload<Args extends unknown[], P, T extends string = string, E = never, M = never> extends BaseActionCreator<P, T, M, E> { /** * Calling this {@link redux#ActionCreator} with `Args` will return * an Action with a payload of type `P` and (depending on the `PrepareAction` * method used) a `meta`- and `error` property of types `M` and `E` respectively. */ (...args: Args): PayloadAction<P, T, M, E>; } /** * Internal version of `ActionCreatorWithPreparedPayload`. Not to be used externally. * * @internal */ declare type _ActionCreatorWithPreparedPayload<PA extends PrepareAction<any> | void, T extends string = string> = PA extends PrepareAction<infer P> ? ActionCreatorWithPreparedPayload<Parameters<PA>, P, T, ReturnType<PA> extends { error: infer E; } ? E : never, ReturnType<PA> extends { meta: infer M; } ? M : never> : void; /** * A builder for an action <-> reducer map. * * @public */ export declare interface ActionReducerMapBuilder<State> { /** * Add a case reducer for actions created by this action creator. * @param actionCreator * @param reducer */ addCase<ActionCreator extends TypedActionCreator<string>>(actionCreator: ActionCreator, reducer: CaseReducer<State, ReturnType<ActionCreator>>): ActionReducerMapBuilder<State>; /** * Add a case reducer for actions with the specified type. * @param type * @param reducer */ addCase<Type extends string, A extends Action<Type>>(type: Type, reducer: CaseReducer<State, A>): ActionReducerMapBuilder<State>; } /** * Defines a mapping from action types to corresponding action object shapes. * * @deprecated This should not be used manually - it is only used for internal * inference purposes and should not have any further value. * It might be removed in the future. * @public */ export declare type Actions<T extends keyof any = string> = Record<T, Action>; /** * returns True if TS version is above 3.5, False if below. * uses feature detection to detect TS version >= 3.5 * * versions below 3.5 will return `{}` for unresolvable interference * * versions above will return `unknown` * * @internal */ declare type AtLeastTS35<True, False> = [True, False][IsUnknown<ReturnType<(<T>() => T)>, 0, 1>]; /** * Basic type for all action creators. * * @inheritdoc {redux#ActionCreator} */ declare interface BaseActionCreator<P, T extends string, M = never, E = never> { type: T; match(action: Action<unknown>): action is PayloadAction<P, T, M, E>; } /** * An *case reducer* is a reducer function for a specific action type. Case * reducers can be composed to full reducers using `createReducer()`. * * Unlike a normal Redux reducer, a case reducer is never called with an * `undefined` state to determine the initial state. Instead, the initial * state is explicitly specified as an argument to `createReducer()`. * * @public */ export declare type CaseReducer<S = any, A extends Action = AnyAction> = (state: S, action: A) => S; /** * Derives the slice's `actions` property from the `reducers` options * * @public */ export declare type CaseReducerActions<CaseReducers extends SliceCaseReducers<any>> = { [Type in keyof CaseReducers]: CaseReducers[Type] extends { prepare: any; } ? ActionCreatorForCaseReducerWithPrepare<CaseReducers[Type]> : ActionCreatorForCaseReducer<CaseReducers[Type]>; }; /** * A mapping from action types to case reducers for `createReducer()`. * * @deprecated This should not be used manually - it is only used * for internal inference purposes and using it manually * would lead to type erasure. * It might be removed in the future. * @public */ export declare type CaseReducers<S, AS extends Actions> = { [T in keyof AS]: AS[T] extends Action ? CaseReducer<S, AS[T]> : void; }; /** * A CaseReducer with a `prepare` method. * * @public */ export declare type CaseReducerWithPrepare<State, Action extends PayloadAction> = { reducer: CaseReducer<State, Action>; prepare: PrepareAction<Action['payload']>; }; /** * Callback function type, to be used in `ConfigureStoreOptions.enhancers` * * @public */ export declare type ConfigureEnhancersCallback = (defaultEnhancers: StoreEnhancer[]) => StoreEnhancer[]; /** * A friendly abstraction over the standard Redux `createStore()` function. * * @param config The store configuration. * @returns A configured Redux store. * * @public */ export declare function configureStore<S = any, A extends Action = AnyAction, M extends Middlewares<S> = [ThunkMiddlewareFor<S>]>(options: ConfigureStoreOptions<S, A, M>): EnhancedStore<S, A, M>; /** * Options for `configureStore()`. * * @public */ export declare interface ConfigureStoreOptions<S = any, A extends Action = AnyAction, M extends Middlewares<S> = Middlewares<S>> { /** * A single reducer function that will be used as the root reducer, or an * object of slice reducers that will be passed to `combineReducers()`. */ reducer: Reducer<S, A> | ReducersMapObject<S, A>; /** * An array of Redux middleware to install. If not supplied, defaults to * the set of middleware returned by `getDefaultMiddleware()`. */ middleware?: M; /** * Whether to enable Redux DevTools integration. Defaults to `true`. * * Additional configuration can be done by passing Redux DevTools options */ devTools?: boolean | EnhancerOptions; /** * The initial state, same as Redux's createStore. * You may optionally specify it to hydrate the state * from the server in universal apps, or to restore a previously serialized * user session. If you use `combineReducers()` to produce the root reducer * function (either directly or indirectly by passing an object as `reducer`), * this must be an object with the same shape as the reducer map keys. */ preloadedState?: DeepPartial<S extends any ? S : S>; /** * The store enhancers to apply. See Redux's `createStore()`. * All enhancers will be included before the DevTools Extension enhancer. * If you need to customize the order of enhancers, supply a callback * function that will receive the original array (ie, `[applyMiddleware]`), * and should return a new array (such as `[applyMiddleware, offline]`). * If you only need to add middleware, you can use the `middleware` parameter instaead. */ enhancers?: StoreEnhancer[] | ConfigureEnhancersCallback; } /** * A utility function to create an action creator for the given action type * string. The action creator accepts a single argument, which will be included * in the action object as a field called payload. The action creator function * will also have its toString() overriden so that it returns the action type, * allowing it to be used in reducer logic that is looking for that action type. * * @param type The action type to use for created actions. * @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }. * If this is given, the resulting action creator will pass it's arguments to this method to calculate payload & meta. * * @public */ export declare function createAction<P = void, T extends string = string>(type: T): PayloadActionCreator<P, T>; /** * A utility function to create an action creator for the given action type * string. The action creator accepts a single argument, which will be included * in the action object as a field called payload. The action creator function * will also have its toString() overriden so that it returns the action type, * allowing it to be used in reducer logic that is looking for that action type. * * @param type The action type to use for created actions. * @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }. * If this is given, the resulting action creator will pass it's arguments to this method to calculate payload & meta. * * @public */ export declare function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>; /** * A utility function that allows defining a reducer as a mapping from action * type to *case reducer* functions that handle these action types. The * reducer's initial state is passed as the first argument. * * @param initialState The initial state to be returned by the reducer. * @param actionsMap A mapping from action types to action-type-specific * case reducers. * * @public */ export declare function createReducer<S, CR extends CaseReducers<S, any> = CaseReducers<S, any>>(initialState: S, actionsMap: CR): Reducer<S>; /** * A utility function that allows defining a reducer as a mapping from action * type to *case reducer* functions that handle these action types. The * reducer's initial state is passed as the first argument. * * @param initialState The initial state to be returned by the reducer. * @param builderCallback A callback that receives a *builder* object to define * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`. * * @public */ export declare function createReducer<S>(initialState: S, builderCallback: (builder: ActionReducerMapBuilder<S>) => void): Reducer<S>; export { createSelector } /** * Creates a middleware that, after every state change, checks if the new * state is serializable. If a non-serializable value is found within the * state, an error is printed to the console. * * @param options Middleware options. * * @public */ export declare function createSerializableStateInvariantMiddleware(options?: SerializableStateInvariantMiddlewareOptions): Middleware; /** * 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>; /** * Options for `createSlice()`. * * @public */ export declare 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 to be returned by the slice reducer. */ initialState: 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 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. * Alternatively, a callback that receives a *builder* object to define * case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`. */ extraReducers?: CaseReducers<NoInfer<State>, any> | ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void); } declare type Diff<T, U> = T extends U ? never : T; /** * Combines all dispatch signatures of all middlewares in the array `M` into * one intersected dispatch signature. */ declare type DispatchForMiddlewares<M> = M extends ReadonlyArray<any> ? UnionToIntersection<M[number] extends infer MiddlewareValues ? MiddlewareValues extends Middleware<infer DispatchExt, any, any> ? DispatchExt extends Function ? DispatchExt : never : never : never> : never; /** * A Redux store returned by `configureStore()`. Supports dispatching * side-effectful _thunks_ in addition to plain actions. * * @public */ export declare interface EnhancedStore<S = any, A extends Action = AnyAction, M extends Middlewares<S> = Middlewares<S>> extends Store<S, A> { /** * The `dispatch` method of your store, enhanced by all it's middlewares. * * @inheritdoc */ dispatch: DispatchForMiddlewares<M> & Dispatch<A>; } /** * @public */ export declare function findNonSerializableValue(value: unknown, path?: ReadonlyArray<string>, isSerializable?: (value: unknown) => boolean, getEntries?: (value: unknown) => [string, any][], ignoredPaths?: string[]): NonSerializableValue | false; /** * Returns any array containing the default middleware installed by * `configureStore()`. Useful if you want to configure your store with a custom * `middleware` array but still keep the default set. * * @return The default middleware used by `configureStore()`. * * @public */ export declare function getDefaultMiddleware<S = any, O extends Partial<GetDefaultMiddlewareOptions> = { thunk: true; immutableCheck: true; serializableCheck: true; }>(options?: O): Array<Middleware<{}, S> | ThunkMiddlewareFor<S, O>>; declare interface GetDefaultMiddlewareOptions { thunk?: boolean | ThunkOptions; immutableCheck?: boolean | ImmutableStateInvariantMiddlewareOptions; serializableCheck?: boolean | SerializableStateInvariantMiddlewareOptions; } /** * Returns the action type of the actions created by the passed * `createAction()`-generated action creator (arbitrary action creators * are not supported). * * @param action The action creator whose action type to get. * @returns The action type used by the action creator. * * @public */ export declare function getType<T extends string>(actionCreator: PayloadActionCreator<any, T>): T; /** * @internal */ declare type IfMaybeUndefined<P, True, False> = [undefined] extends [P] ? True : False; declare type IfPrepareActionMethodProvided<PA extends PrepareAction<any> | void, True, False> = PA extends (...args: any[]) => any ? True : False; /** * @internal */ declare type IfVoid<P, True, False> = [void] extends [P] ? True : False; declare interface ImmutableStateInvariantMiddlewareOptions { isImmutable?: (value: any) => boolean; ignore?: string[]; } /** * return True if T is `any`, otherwise return False * taken from https://github.com/joonhocho/tsdef * * @internal */ declare type IsAny<T, True, False = never> = true | false extends (T extends never ? true : false) ? True : False; /** * @internal */ declare type IsEmptyObj<T, True, False = never> = T extends any ? keyof T extends never ? IsUnknown<T, False, IfMaybeUndefined<T, False, IfVoid<T, False, True>>> : False : never; /** * Returns true if the passed value is "plain", i.e. a value that is either * directly JSON-serializable (boolean, number, string, array, plain object) * or `undefined`. * * @param val The value to check. * * @public */ export declare function isPlain(val: any): boolean; /** * return True if T is `unknown`, otherwise return False * taken from https://github.com/joonhocho/tsdef * * @internal */ declare type IsUnknown<T, True, False = never> = unknown extends T ? IsAny<T, False, True> : False; /** * @internal */ declare type IsUnknownOrNonInferrable<T, True, False> = AtLeastTS35<IsUnknown<T, True, False>, IsEmptyObj<T, True, IsUnknown<T, True, False>>>; declare type Middlewares<S> = ReadonlyArray<Middleware<{}, S>>; /** * Helper type. Passes T out again, but boxes it in a way that it cannot * "widen" the type by accident if it is a generic that should be inferred * from elsewhere. * * @internal */ declare type NoInfer<T> = [T][T extends any ? 0 : never]; declare interface NonSerializableValue { keyPath: string; value: unknown; } declare type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; /** * An action with a string type and an associated payload. This is the * type of action returned by `createAction()` action creators. * * @template P The type of the action's payload. * @template T the type used for the action type. * @template M The type of the action's meta (optional) * @template E The type of the action's error (optional) * * @public */ export declare type PayloadAction<P = void, T extends string = string, M = never, E = never> = { payload: P; type: T; } & ([M] extends [never] ? {} : { meta: M; }) & ([E] extends [never] ? {} : { error: E; }); /** * An action creator that produces actions with a `payload` attribute. * * @typeParam P the `payload` type * @typeParam T the `type` of the resulting action * @typeParam PA if the resulting action is preprocessed by a `prepare` method, the signature of said method. * * @public */ export declare type PayloadActionCreator<P = void, T extends string = string, PA extends PrepareAction<P> | void = void> = IfPrepareActionMethodProvided<PA, _ActionCreatorWithPreparedPayload<PA, T>, IsAny<P, ActionCreatorWithPayload<any, T>, IsUnknownOrNonInferrable<P, ActionCreatorWithNonInferrablePayload<T>, IfVoid<P, ActionCreatorWithoutPayload<T>, IfMaybeUndefined<P, ActionCreatorWithOptionalPayload<P, T>, ActionCreatorWithPayload<P, T>>>>>>; /** * A "prepare" method to be used as the second parameter of `createAction`. * Takes any number of arguments and returns a Flux Standard Action without * type (will be added later) that *must* contain a payload (might be undefined). * * @public */ export declare type PrepareAction<P> = ((...args: any[]) => { payload: P; }) | ((...args: any[]) => { payload: P; meta: any; }) | ((...args: any[]) => { payload: P; error: any; }) | ((...args: any[]) => { payload: P; meta: any; error: any; }); /** * Options for `createSerializableStateInvariantMiddleware()`. * * @public */ export declare interface SerializableStateInvariantMiddlewareOptions { /** * The function to check if a value is considered serializable. This * function is applied recursively to every value contained in the * state. Defaults to `isPlain()`. */ isSerializable?: (value: any) => boolean; /** * The function that will be used to retrieve entries from each * value. If unspecified, `Object.entries` will be used. Defaults * to `undefined`. */ getEntries?: (value: any) => [string, any][]; /** * An array of action types to ignore when checking for serializability, Defaults to [] */ ignoredActions?: string[]; /** * An array of dot-separated path strings to ignore when checking for serializability, Defaults to [] */ ignoredPaths?: string[]; } /** * The return value of `createSlice` * * @public */ export declare 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>; /** * 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>; } /** * An action creator atttached to a slice. * * @deprecated please use PayloadActionCreator directly * * @public */ export declare type SliceActionCreator<P> = PayloadActionCreator<P>; /** * 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>>; }; /** * 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]; }; export { ThunkAction } declare type ThunkMiddlewareFor<S, O extends GetDefaultMiddlewareOptions = {}> = O extends { thunk: false; } ? never : O extends { thunk: { extraArgument: infer E; }; } ? ThunkMiddleware<S, AnyAction, E> : ThunkMiddleware<S, AnyAction, null> | ThunkMiddleware<S, AnyAction>; declare interface ThunkOptions<E = any> { extraArgument: E; } declare interface TypedActionCreator<Type extends string> { (...args: any[]): Action<Type>; type: Type; } /** * Convert a Union type `(A|B)` to and intersecion type `(A&B)` */ declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; /** * 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'>; } : {}; }; export * from "redux"; export { }