|
| 1 | +import { |
| 2 | + Action, |
| 3 | + ActionCreatorsMapObject, |
| 4 | + AnyAction, |
| 5 | + Dispatch, |
| 6 | + Middleware, |
| 7 | +} from 'redux'; |
| 8 | + |
| 9 | +export interface ThunkDispatch<S, E, A extends Action> { |
| 10 | + <R>(thunkAction: ThunkAction<R, S, E, A>): R; |
| 11 | + <T extends A>(action: T): T; |
| 12 | +} |
| 13 | + |
| 14 | +export type ThunkAction<R, S, E, A extends Action> = ( |
| 15 | + dispatch: ThunkDispatch<S, E, A>, |
| 16 | + getState: () => S, |
| 17 | + extraArgument: E |
| 18 | +) => R; |
| 19 | + |
| 20 | +/** |
| 21 | + * Takes a ThunkAction and returns a function signature which matches how it would appear when processed using |
| 22 | + * bindActionCreators |
| 23 | + * |
| 24 | + * @template T ThunkAction to be wrapped |
| 25 | + */ |
| 26 | +export type ThunkActionDispatch< |
| 27 | +T extends (...args: any[]) => ThunkAction<any, any, any, any> |
| 28 | +> = (...args: Parameters<T>) => ReturnType<ReturnType<T>>; |
| 29 | + |
| 30 | +export type ThunkMiddleware< |
| 31 | +S = {}, |
| 32 | +A extends Action = AnyAction, |
| 33 | +E = undefined |
| 34 | +> = Middleware<ThunkDispatch<S, E, A>, S, ThunkDispatch<S, E, A>>; |
| 35 | + |
| 36 | +declare const thunk: ThunkMiddleware & { |
| 37 | +withExtraArgument<E>(extraArgument: E): ThunkMiddleware<{}, AnyAction, E>; |
| 38 | +}; |
| 39 | + |
| 40 | +export default thunk; |
| 41 | + |
| 42 | +/** |
| 43 | + * Redux behaviour changed by middleware, so overloads here |
| 44 | + */ |
| 45 | +declare module 'redux' { |
| 46 | + /** |
| 47 | + * Overload for bindActionCreators redux function, returns expects responses |
| 48 | + * from thunk actions |
| 49 | + */ |
| 50 | + function bindActionCreators<M extends ActionCreatorsMapObject<any>>( |
| 51 | + actionCreators: M, |
| 52 | + dispatch: Dispatch |
| 53 | + ): { |
| 54 | + [N in keyof M]: ReturnType<M[N]> extends ThunkAction<any, any, any, any> |
| 55 | + ? (...args: Parameters<M[N]>) => ReturnType<ReturnType<M[N]>> |
| 56 | + : M[N] |
| 57 | + }; |
| 58 | +} |
0 commit comments