types.d.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. export 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. export declare 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. export declare 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 `thunk.withExtraArgument()`)
  49. */
  50. export declare type ThunkMiddleware<State = any, BasicAction extends Action = AnyAction, ExtraThunkArg = undefined> = Middleware<ThunkDispatch<State, ExtraThunkArg, BasicAction>, State, ThunkDispatch<State, ExtraThunkArg, BasicAction>>;