import type { Dispatch, AnyAction } from 'redux'; import type { ActionCreatorWithPreparedPayload } from './createAction'; import type { ThunkDispatch } from 'redux-thunk'; import type { FallbackIfUnknown, IsAny, IsUnknown } from './tsHelpers'; export declare type BaseThunkAPI = { dispatch: D; getState: () => S; extra: E; requestId: string; signal: AbortSignal; rejectWithValue: IsUnknown RejectWithValue, (value: RejectedValue, meta: RejectedMeta) => RejectWithValue>; fulfillWithValue: IsUnknown(value: FulfilledValue) => FulfillWithMeta, (value: FulfilledValue, meta: FulfilledMeta) => FulfillWithMeta>; }; /** * @public */ export interface SerializedError { name?: string; message?: string; stack?: string; code?: string; } declare class RejectWithValue { readonly payload: Payload; readonly meta: RejectedMeta; private readonly _type; constructor(payload: Payload, meta: RejectedMeta); } declare class FulfillWithMeta { readonly payload: Payload; readonly meta: FulfilledMeta; private readonly _type; constructor(payload: Payload, meta: FulfilledMeta); } /** * Serializes an error into a plain object. * Reworked from https://github.com/sindresorhus/serialize-error * * @public */ export declare const miniSerializeError: (value: any) => SerializedError; declare type AsyncThunkConfig = { state?: unknown; dispatch?: Dispatch; extra?: unknown; rejectValue?: unknown; serializedErrorType?: unknown; pendingMeta?: unknown; fulfilledMeta?: unknown; rejectedMeta?: unknown; }; declare type GetState = ThunkApiConfig extends { state: infer State; } ? State : unknown; declare type GetExtra = ThunkApiConfig extends { extra: infer Extra; } ? Extra : unknown; declare type GetDispatch = ThunkApiConfig extends { dispatch: infer Dispatch; } ? FallbackIfUnknown, GetExtra, AnyAction>> : ThunkDispatch, GetExtra, AnyAction>; declare type GetThunkAPI = BaseThunkAPI, GetExtra, GetDispatch, GetRejectValue, GetRejectedMeta, GetFulfilledMeta>; declare type GetRejectValue = ThunkApiConfig extends { rejectValue: infer RejectValue; } ? RejectValue : unknown; declare type GetPendingMeta = ThunkApiConfig extends { pendingMeta: infer PendingMeta; } ? PendingMeta : unknown; declare type GetFulfilledMeta = ThunkApiConfig extends { fulfilledMeta: infer FulfilledMeta; } ? FulfilledMeta : unknown; declare type GetRejectedMeta = ThunkApiConfig extends { rejectedMeta: infer RejectedMeta; } ? RejectedMeta : unknown; declare type GetSerializedErrorType = ThunkApiConfig extends { serializedErrorType: infer GetSerializedErrorType; } ? GetSerializedErrorType : SerializedError; declare type MaybePromise = T | Promise; /** * A type describing the return value of the `payloadCreator` argument to `createAsyncThunk`. * Might be useful for wrapping `createAsyncThunk` in custom abstractions. * * @public */ export declare type AsyncThunkPayloadCreatorReturnValue = MaybePromise, Returned, FulfillWithMeta>> | RejectWithValue, GetRejectedMeta>>; /** * A type describing the `payloadCreator` argument to `createAsyncThunk`. * Might be useful for wrapping `createAsyncThunk` in custom abstractions. * * @public */ export declare type AsyncThunkPayloadCreator = (arg: ThunkArg, thunkAPI: GetThunkAPI) => AsyncThunkPayloadCreatorReturnValue; /** * A ThunkAction created by `createAsyncThunk`. * Dispatching it returns a Promise for either a * fulfilled or rejected action. * Also, the returned value contains an `abort()` method * that allows the asyncAction to be cancelled from the outside. * * @public */ export declare type AsyncThunkAction = (dispatch: GetDispatch, getState: () => GetState, extra: GetExtra) => Promise> | ReturnType>> & { abort: (reason?: string) => void; requestId: string; arg: ThunkArg; unwrap: () => Promise; }; declare type AsyncThunkActionCreator = IsAny AsyncThunkAction, unknown extends ThunkArg ? (arg: ThunkArg) => AsyncThunkAction : [ThunkArg] extends [void] | [undefined] ? () => AsyncThunkAction : [void] extends [ThunkArg] ? (arg?: ThunkArg) => AsyncThunkAction : [undefined] extends [ThunkArg] ? WithStrictNullChecks<(arg?: ThunkArg) => AsyncThunkAction, (arg: ThunkArg) => AsyncThunkAction> : (arg: ThunkArg) => AsyncThunkAction>; /** * Options object for `createAsyncThunk`. * * @public */ export declare type AsyncThunkOptions = { /** * A method to control whether the asyncThunk should be executed. Has access to the * `arg`, `api.getState()` and `api.extra` arguments. * * @returns `false` if it should be skipped */ condition?(arg: ThunkArg, api: Pick, 'getState' | 'extra'>): boolean | undefined; /** * If `condition` returns `false`, the asyncThunk will be skipped. * This option allows you to control whether a `rejected` action with `meta.condition == false` * will be dispatched or not. * * @default `false` */ dispatchConditionRejection?: boolean; serializeError?: (x: unknown) => GetSerializedErrorType; /** * A function to use when generating the `requestId` for the request sequence. * * @default `nanoid` */ idGenerator?: () => string; } & IsUnknown, { /** * A method to generate additional properties to be added to `meta` of the pending action. * * Using this optional overload will not modify the types correctly, this overload is only in place to support JavaScript users. * Please use the `ThunkApiConfig` parameter `pendingMeta` to get access to a correctly typed overload */ getPendingMeta?(base: { arg: ThunkArg; requestId: string; }, api: Pick, 'getState' | 'extra'>): GetPendingMeta; }, { /** * A method to generate additional properties to be added to `meta` of the pending action. */ getPendingMeta(base: { arg: ThunkArg; requestId: string; }, api: Pick, 'getState' | 'extra'>): GetPendingMeta; }>; export declare type AsyncThunkPendingActionCreator = ActionCreatorWithPreparedPayload<[ string, ThunkArg, GetPendingMeta? ], undefined, string, never, { arg: ThunkArg; requestId: string; requestStatus: 'pending'; } & GetPendingMeta>; export declare type AsyncThunkRejectedActionCreator = ActionCreatorWithPreparedPayload<[ Error | null, string, ThunkArg, GetRejectValue?, GetRejectedMeta? ], GetRejectValue | undefined, string, GetSerializedErrorType, { arg: ThunkArg; requestId: string; requestStatus: 'rejected'; aborted: boolean; condition: boolean; } & (({ rejectedWithValue: false; } & { [K in keyof GetRejectedMeta]?: undefined; }) | ({ rejectedWithValue: true; } & GetRejectedMeta))>; export declare type AsyncThunkFulfilledActionCreator = ActionCreatorWithPreparedPayload<[ Returned, string, ThunkArg, GetFulfilledMeta? ], Returned, string, never, { arg: ThunkArg; requestId: string; requestStatus: 'fulfilled'; } & GetFulfilledMeta>; /** * A type describing the return value of `createAsyncThunk`. * Might be useful for wrapping `createAsyncThunk` in custom abstractions. * * @public */ export declare type AsyncThunk = AsyncThunkActionCreator & { pending: AsyncThunkPendingActionCreator; rejected: AsyncThunkRejectedActionCreator; fulfilled: AsyncThunkFulfilledActionCreator; typePrefix: string; }; /** * * @param typePrefix * @param payloadCreator * @param options * * @public */ export declare function createAsyncThunk(typePrefix: string, payloadCreator: AsyncThunkPayloadCreator, options?: AsyncThunkOptions): AsyncThunk; interface UnwrappableAction { payload: any; meta?: any; error?: any; } declare type UnwrappedActionPayload = Exclude['payload']; /** * @public */ export declare function unwrapResult(action: R): UnwrappedActionPayload; declare type WithStrictNullChecks = undefined extends boolean ? False : True; export {};