@@ -3,21 +3,26 @@ import {
3
3
applyMiddleware , createStore , Dispatch , Reducer , Action
4
4
} from "../../index.d.ts" ;
5
5
6
+ declare module "../../index.d.ts" {
7
+ export interface Dispatch < S > {
8
+ < R > ( asyncAction : ( dispatch : Dispatch < S > , getState : ( ) => S ) => R ) : R ;
9
+ }
10
+ }
6
11
7
- type Thunk < S , O > = ( dispatch : Dispatch , getState ? : ( ) => S ) => O ;
12
+ type Thunk < S , O > = ( dispatch : Dispatch < S > , getState : ( ) => S ) => O ;
8
13
9
14
const thunkMiddleware : Middleware =
10
15
< S > ( { dispatch, getState} : MiddlewareAPI < S > ) =>
11
- ( next : Dispatch ) =>
12
- < A , B > ( action : A | Thunk < S , B > ) : B =>
16
+ ( next : Dispatch < S > ) =>
17
+ < A extends Action , B > ( action : A | Thunk < S , B > ) : B | Action =>
13
18
typeof action === 'function' ?
14
19
( < Thunk < S , B > > action ) ( dispatch , getState ) :
15
- < B > next ( < A > action )
20
+ next ( < A > action )
16
21
17
22
18
23
const loggerMiddleware : Middleware =
19
24
< S > ( { getState} : MiddlewareAPI < S > ) =>
20
- ( next : Dispatch ) =>
25
+ ( next : Dispatch < S > ) =>
21
26
( action : any ) : any => {
22
27
console . log ( 'will dispatch' , action )
23
28
@@ -47,7 +52,7 @@ const storeWithThunkMiddleware = createStore(
47
52
) ;
48
53
49
54
storeWithThunkMiddleware . dispatch (
50
- ( dispatch : Dispatch , getState : ( ) => State ) => {
55
+ ( dispatch , getState ) => {
51
56
const todos : string [ ] = getState ( ) . todos ;
52
57
dispatch ( { type : 'ADD_TODO' } )
53
58
}
0 commit comments