1
1
import { createStore , applyMiddleware } from 'redux' ;
2
- import thunk , { ThunkAction } from '../index' ;
2
+ import thunk , { ThunkAction , ThunkMiddleware } from '../index' ;
3
3
4
4
type State = {
5
5
foo : string ;
6
6
} ;
7
7
8
- type Actions = { type : 'FOO' } ;
8
+ type Actions = { type : 'FOO' } | { type : 'BAR' , result : number } ;
9
+
10
+ type ThunkResult < R > = ThunkAction < R , State , undefined , Actions > ;
9
11
10
12
const initialState : State = {
11
13
foo : 'foo'
@@ -15,42 +17,52 @@ function fakeReducer(state: State = initialState, action: Actions): State {
15
17
return state ;
16
18
}
17
19
18
- const store = createStore ( fakeReducer , applyMiddleware ( thunk ) ) ;
20
+ const store = createStore ( fakeReducer , applyMiddleware ( thunk as ThunkMiddleware < State , Actions > ) ) ;
19
21
20
22
store . dispatch ( dispatch => {
21
23
dispatch ( { type : 'FOO' } ) ;
24
+ // typings:expect-error
25
+ dispatch ( { type : 'BAR' } )
26
+ dispatch ( { type : 'BAR' , result : 5 } )
27
+ // typings:expect-error
28
+ store . dispatch ( { type : 'BAZ' } ) ;
22
29
} ) ;
23
30
24
- function testGetState ( ) : ThunkAction < void , State , { } , Actions > {
31
+ function testGetState ( ) : ThunkResult < void > {
25
32
return ( dispatch , getState ) => {
26
33
const state = getState ( ) ;
27
34
const foo : string = state . foo ;
35
+ dispatch ( { type : 'FOO' } ) ;
36
+ // typings:expect-error
37
+ dispatch ( { type : 'BAR' } ) ;
38
+ dispatch ( { type : 'BAR' , result : 5 } ) ;
39
+ // typings:expect-error
40
+ dispatch ( { type : 'BAZ' } ) ;
41
+ // Can dispatch another thunk action
42
+ dispatch ( testGetState ( ) ) ;
28
43
} ;
29
44
}
30
45
46
+ store . dispatch ( { type : 'FOO' } ) ;
47
+ // typings:expect-error
48
+ store . dispatch ( { type : 'BAR' } )
49
+ store . dispatch ( { type : 'BAR' , result : 5 } )
50
+ // typings:expect-error
51
+ store . dispatch ( { type : 'BAZ' } ) ;
31
52
store . dispatch ( testGetState ( ) ) ;
32
53
33
54
const storeThunkArg = createStore (
34
55
fakeReducer ,
35
- applyMiddleware ( thunk . withExtraArgument ( 'bar' ) )
56
+ applyMiddleware ( thunk . withExtraArgument ( 'bar' ) as ThunkMiddleware < State , Actions , string > )
36
57
) ;
37
58
38
59
storeThunkArg . dispatch ( ( dispatch , getState , extraArg ) => {
39
60
const bar : string = extraArg ;
61
+ store . dispatch ( { type : 'FOO' } ) ;
62
+ // typings:expect-error
63
+ store . dispatch ( { type : 'BAR' } )
64
+ store . dispatch ( { type : 'BAR' , result : 5 } )
65
+ // typings:expect-error
66
+ store . dispatch ( { type : 'BAZ' } ) ;
40
67
console . log ( extraArg ) ;
41
68
} ) ;
42
-
43
- const thunkAction : ThunkAction < void , State , { bar : number } , Actions > = (
44
- dispatch ,
45
- getState ,
46
- extraArg
47
- ) => {
48
- const foo : string = getState ( ) . foo ;
49
- const bar : number = extraArg . bar ;
50
-
51
- dispatch ( { type : 'FOO' } ) ;
52
- } ;
53
-
54
- const thunkActionDispatchOnly : ThunkAction < void , { } , { } , Actions > = dispatch => {
55
- dispatch ( { type : 'FOO' } ) ;
56
- } ;
0 commit comments