| 1 | import type { 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 | export interface ThunkDispatch<
|
|---|
| 15 | State,
|
|---|
| 16 | ExtraThunkArg,
|
|---|
| 17 | BasicAction extends Action
|
|---|
| 18 | > {
|
|---|
| 19 | // When the thunk middleware is added, `store.dispatch` now has three overloads (NOTE: the order here matters for correct behavior and is very fragile - do not reorder these!):
|
|---|
| 20 |
|
|---|
| 21 | // 1) The specific thunk function overload
|
|---|
| 22 | /** Accepts a thunk function, runs it, and returns whatever the thunk itself returns */
|
|---|
| 23 | <ReturnType>(
|
|---|
| 24 | thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>
|
|---|
| 25 | ): ReturnType
|
|---|
| 26 |
|
|---|
| 27 | // 2) The base overload.
|
|---|
| 28 | /** Accepts a standard action object, and returns that action object */
|
|---|
| 29 | <Action extends BasicAction>(action: Action): Action
|
|---|
| 30 |
|
|---|
| 31 | // 3) A union of the other two overloads. This overload exists to work around a problem
|
|---|
| 32 | // with TS inference ( see https://github.com/microsoft/TypeScript/issues/14107 )
|
|---|
| 33 | /** A union of the other two overloads for TS inference purposes */
|
|---|
| 34 | <ReturnType, Action extends BasicAction>(
|
|---|
| 35 | action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>
|
|---|
| 36 | ): Action | ReturnType
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * A "thunk" action (a callback function that can be dispatched to the Redux
|
|---|
| 41 | * store.)
|
|---|
| 42 | *
|
|---|
| 43 | * Also known as the "thunk inner function", when used with the typical pattern
|
|---|
| 44 | * of an action creator function that returns a thunk action.
|
|---|
| 45 | *
|
|---|
| 46 | * @template ReturnType The return type of the thunk's inner function
|
|---|
| 47 | * @template State The redux state
|
|---|
| 48 | * @template ExtraThunkArg Optional extra argument passed to the inner function
|
|---|
| 49 | * (if specified when setting up the Thunk middleware)
|
|---|
| 50 | * @template BasicAction The (non-thunk) actions that can be dispatched.
|
|---|
| 51 | */
|
|---|
| 52 | export type ThunkAction<
|
|---|
| 53 | ReturnType,
|
|---|
| 54 | State,
|
|---|
| 55 | ExtraThunkArg,
|
|---|
| 56 | BasicAction extends Action
|
|---|
| 57 | > = (
|
|---|
| 58 | dispatch: ThunkDispatch<State, ExtraThunkArg, BasicAction>,
|
|---|
| 59 | getState: () => State,
|
|---|
| 60 | extraArgument: ExtraThunkArg
|
|---|
| 61 | ) => ReturnType
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * A generic type that takes a thunk action creator and returns a function
|
|---|
| 65 | * signature which matches how it would appear after being processed using
|
|---|
| 66 | * bindActionCreators(): a function that takes the arguments of the outer
|
|---|
| 67 | * function, and returns the return type of the inner "thunk" function.
|
|---|
| 68 | *
|
|---|
| 69 | * @template ActionCreator Thunk action creator to be wrapped
|
|---|
| 70 | */
|
|---|
| 71 | export type ThunkActionDispatch<
|
|---|
| 72 | ActionCreator extends (...args: any[]) => ThunkAction<any, any, any, any>
|
|---|
| 73 | > = (
|
|---|
| 74 | ...args: Parameters<ActionCreator>
|
|---|
| 75 | ) => ReturnType<ReturnType<ActionCreator>>
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * @template State The redux state
|
|---|
| 79 | * @template BasicAction The (non-thunk) actions that can be dispatched
|
|---|
| 80 | * @template ExtraThunkArg An optional extra argument to pass to a thunk's
|
|---|
| 81 | * inner function. (Only used if you call `withExtraArgument()`)
|
|---|
| 82 | */
|
|---|
| 83 | export type ThunkMiddleware<
|
|---|
| 84 | State = any,
|
|---|
| 85 | BasicAction extends Action = AnyAction,
|
|---|
| 86 | ExtraThunkArg = undefined
|
|---|
| 87 | > = Middleware<
|
|---|
| 88 | ThunkDispatch<State, ExtraThunkArg, BasicAction>,
|
|---|
| 89 | State,
|
|---|
| 90 | ThunkDispatch<State, ExtraThunkArg, BasicAction>
|
|---|
| 91 | >
|
|---|