Skip to content

Instantly share code, notes, and snippets.

@jaredh159
Created August 30, 2022 18:26
Show Gist options
  • Save jaredh159/e212b07c18761aa4d2f42d580d803442 to your computer and use it in GitHub Desktop.
Save jaredh159/e212b07c18761aa4d2f42d580d803442 to your computer and use it in GitHub Desktop.
CreateAsyncThunk typed fn
import {
AnyAction,
ThunkAction as LibThunkAction,
createAsyncThunk as libCreateAsycThunk,
AsyncThunkPayloadCreator,
AsyncThunkOptions,
createAction,
nanoid,
ThunkDispatch,
Action,
ActionCreatorWithPreparedPayload,
} from '@reduxjs/toolkit';
import Result from '../api/Result';
import type { State, Dispatch } from './store';
export function createAsyncThunk<
Returned,
ThunkArg = void,
ThunkApiConfig extends AsyncThunkConfig = { state: State },
>(
typePrefix: string,
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>,
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>,
): ReturnType<typeof libCreateAsycThunk<Returned, ThunkArg, ThunkApiConfig>> {
return libCreateAsycThunk<Returned, ThunkArg, ThunkApiConfig>(
typePrefix,
payloadCreator,
options,
);
}
// copied from @reduxjs/toolkit, not exported for some reason...
type AsyncThunkConfig = {
state?: unknown;
dispatch?: Dispatch;
extra?: unknown;
rejectValue?: unknown;
serializedErrorType?: unknown;
pendingMeta?: unknown;
fulfilledMeta?: unknown;
rejectedMeta?: unknown;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment