11import { Middleware , GetState , Dispatch , Reducer , Store } from '../types' ;
22
33export interface ThunkMiddleware < State , ExtraArgument >
4- extends Middleware < State , Reducer < State > , Thunk < State , ExtraArgument , any > > {
4+ extends Middleware < State , Reducer < State > , Thunk < State , ExtraArgument > > {
5+ ( store : Store < State , Reducer < State > | Delegate < State , ExtraArgument > > ) : {
6+ ( next : Dispatch < Reducer < State > , State > ) : Dispatch < Delegate < State , ExtraArgument > > ;
7+ } ;
58 withExtraArgument : < EA > ( extraArgument : EA ) => ThunkMiddleware < State , EA > ;
69}
710
8- export interface Delegate < State , ExtraArgument , Return > {
11+ export interface Delegate < State , ExtraArgument , Return = any > {
912 ( dispatch : ThunkDispatch < State , ExtraArgument > , getState : GetState < State > , extraArgument : ExtraArgument ) : Return ;
1013}
1114
12- export interface ThunkDispatch < State , ExtraArgument > extends Dispatch < Reducer < State > > {
13- < Return > ( reducer : Thunk < State , ExtraArgument , Return > ) : Return ;
15+ export interface Thunk < State , ExtraArgument , Return = any > {
16+ ( state : State ) : Delegate < State , ExtraArgument , Return > ;
1417}
1518
16- export interface Thunk < State , ExtraArgument , Return > {
17- ( state : State ) : Delegate < State , ExtraArgument , Return > ;
19+ export interface ThunkDispatch < State , ExtraArgument > extends Dispatch < Reducer < State > , State > {
20+ < Return > ( thunk : Thunk < State , ExtraArgument , Return > ) : Return ;
1821}
1922
20- const thunkFactory = ( extraArgument ?) => {
21- const thunk = store => next => reducer => {
23+ const thunkFactory = < State , ExtraArgument > ( extraArgument ?: ExtraArgument ) => {
24+ const thunk = ( store => next => reducer => {
2225 if ( typeof reducer !== 'function' ) throw new Error ( 'Thunk requires reducers as functions' ) ;
2326 const state = store . getState ( ) ;
2427 const result = reducer ( state ) ;
@@ -27,8 +30,8 @@ const thunkFactory = (extraArgument?) => {
2730 next ( _ => result ) ;
2831 return reducer ;
2932 }
30- } ;
31- thunk [ ' withExtraArgument' ] = thunkFactory ;
33+ } ) as ThunkMiddleware < State , ExtraArgument > ;
34+ thunk . withExtraArgument = thunkFactory ;
3235 return thunk ;
3336} ;
3437
0 commit comments