| 1 | import { Action, AnyAction, Middleware } from 'redux';
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * The dispatch method as modified by React-Thunk; overloaded so that you can
|
|---|
| 5 | * dispatch:
|
|---|
| 6 | * - standard (object) actions: `dispatch()` returns the action itself
|
|---|
| 7 | * - thunk actions: `dispatch()` returns the thunk's return value
|
|---|
| 8 | *
|
|---|
| 9 | * @template State The redux state
|
|---|
| 10 | * @template ExtraThunkArg The extra argument passed to the inner function of
|
|---|
| 11 | * thunks (if specified when setting up the Thunk middleware)
|
|---|
| 12 | * @template BasicAction The (non-thunk) actions that can be dispatched.
|
|---|
| 13 | */
|
|---|
| 14 | interface ThunkDispatch<State, ExtraThunkArg, BasicAction extends Action> {
|
|---|
| 15 | /** Accepts a thunk function, runs it, and returns whatever the thunk itself returns */
|
|---|
| 16 | <ReturnType>(thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>): ReturnType;
|
|---|
| 17 | /** Accepts a standard action object, and returns that action object */
|
|---|
| 18 | <Action extends BasicAction>(action: Action): Action;
|
|---|
| 19 | /** A union of the other two overloads for TS inference purposes */
|
|---|
| 20 | <ReturnType, Action extends BasicAction>(action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>): Action | ReturnType;
|
|---|
| 21 | }
|
|---|
| 22 | /**
|
|---|
| 23 | * A "thunk" action (a callback function that can be dispatched to the Redux
|
|---|
| 24 | * store.)
|
|---|
| 25 | *
|
|---|
| 26 | * Also known as the "thunk inner function", when used with the typical pattern
|
|---|
| 27 | * of an action creator function that returns a thunk action.
|
|---|
| 28 | *
|
|---|
| 29 | * @template ReturnType The return type of the thunk's inner function
|
|---|
| 30 | * @template State The redux state
|
|---|
| 31 | * @template ExtraThunkArg Optional extra argument passed to the inner function
|
|---|
| 32 | * (if specified when setting up the Thunk middleware)
|
|---|
| 33 | * @template BasicAction The (non-thunk) actions that can be dispatched.
|
|---|
| 34 | */
|
|---|
| 35 | type ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction extends Action> = (dispatch: ThunkDispatch<State, ExtraThunkArg, BasicAction>, getState: () => State, extraArgument: ExtraThunkArg) => ReturnType;
|
|---|
| 36 | /**
|
|---|
| 37 | * A generic type that takes a thunk action creator and returns a function
|
|---|
| 38 | * signature which matches how it would appear after being processed using
|
|---|
| 39 | * bindActionCreators(): a function that takes the arguments of the outer
|
|---|
| 40 | * function, and returns the return type of the inner "thunk" function.
|
|---|
| 41 | *
|
|---|
| 42 | * @template ActionCreator Thunk action creator to be wrapped
|
|---|
| 43 | */
|
|---|
| 44 | type ThunkActionDispatch<ActionCreator extends (...args: any[]) => ThunkAction<any, any, any, any>> = (...args: Parameters<ActionCreator>) => ReturnType<ReturnType<ActionCreator>>;
|
|---|
| 45 | /**
|
|---|
| 46 | * @template State The redux state
|
|---|
| 47 | * @template BasicAction The (non-thunk) actions that can be dispatched
|
|---|
| 48 | * @template ExtraThunkArg An optional extra argument to pass to a thunk's
|
|---|
| 49 | * inner function. (Only used if you call `withExtraArgument()`)
|
|---|
| 50 | */
|
|---|
| 51 | type ThunkMiddleware<State = any, BasicAction extends Action = AnyAction, ExtraThunkArg = undefined> = Middleware<ThunkDispatch<State, ExtraThunkArg, BasicAction>, State, ThunkDispatch<State, ExtraThunkArg, BasicAction>>;
|
|---|
| 52 |
|
|---|
| 53 | /** A function that accepts a potential "extra argument" value to be injected later,
|
|---|
| 54 | * and returns an instance of the thunk middleware that uses that value
|
|---|
| 55 | */
|
|---|
| 56 | declare function createThunkMiddleware<State = any, BasicAction extends Action = AnyAction, ExtraThunkArg = undefined>(extraArgument?: ExtraThunkArg): ThunkMiddleware<State, BasicAction, ExtraThunkArg>;
|
|---|
| 57 | declare const thunk: ThunkMiddleware<any, AnyAction, undefined>;
|
|---|
| 58 | declare const withExtraArgument: typeof createThunkMiddleware;
|
|---|
| 59 |
|
|---|
| 60 | export { ThunkAction, ThunkActionDispatch, ThunkDispatch, ThunkMiddleware, thunk, withExtraArgument };
|
|---|