redux-thunk.d.ts 3.5 KB

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