Skip to content

Commit 1259b21

Browse files
committed
added redux-thunk types overload
1 parent 8c796de commit 1259b21

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

playground/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// "@src/*": ["src/*"] // will enable import aliases -> import { ... } from '@src/components'
66
// WARNING: Require to add this to your webpack config -> resolve: { alias: { '@src': PATH_TO_SRC } }
77
// "redux": ["typings/redux"], // override library types with your alternative type-definitions in typings folder
8+
"redux-thunk": ["typings/redux-thunk"] // override library types with your alternative type-definitions in typings folder
89
},
910
"outDir": "dist/", // target for compiled files
1011
"allowSyntheticDefaultImports": true, // no errors with commonjs modules interop
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)