Skip to content

Commit f86bd3f

Browse files
author
Wang Zhongliang
committed
Fix typings and test cases
1 parent 196df61 commit f86bd3f

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export type ThunkAction<R, S, E, A extends Action> = (
1111
extraArgument: E
1212
) => R;
1313

14-
type ThunkMiddleware<E> = Middleware<ThunkDispatch<S, E, A>, S, ThunkDispatch<S, E, A>>;
14+
export type ThunkMiddleware<S = {}, A extends Action = AnyAction, E = undefined> = Middleware<ThunkDispatch<S, E, A>, S, ThunkDispatch<S, E, A>>;
1515

16-
declare const thunk: ThunkMiddleware<undefined> & {
17-
withExtraArgument<E>(extraArgument: E): ThunkMiddleware<E>
18-
};
16+
declare const thunk: ThunkMiddleware & {
17+
withExtraArgument<E>(extraArgument: E): ThunkMiddleware<{}, AnyAction, E>
18+
}
1919

2020
export default thunk;

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chai from 'chai';
22
import thunkMiddleware from '../src/index';
3-
import { check } from 'typings-tester';
3+
import { checkDirectory } from 'typings-tester';
44

55

66
describe('thunk middleware', () => {
@@ -98,7 +98,7 @@ describe('thunk middleware', () => {
9898
this.timeout(0);
9999

100100
it('should compile against index.d.ts', () => {
101-
check([__dirname + '/typescript.ts'], __dirname + '/tsconfig.json');
101+
checkDirectory(__dirname);
102102
});
103103
});
104104
});

test/typescript.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { createStore, applyMiddleware } from 'redux';
2-
import thunk, { ThunkAction } from '../index';
2+
import thunk, { ThunkAction, ThunkMiddleware } from '../index';
33

44
type State = {
55
foo: string;
66
};
77

8-
type Actions = { type: 'FOO' };
8+
type Actions = { type: 'FOO' } | { type: 'BAR', result: number };
9+
10+
type ThunkResult<R> = ThunkAction<R, State, undefined, Actions>;
911

1012
const initialState: State = {
1113
foo: 'foo'
@@ -15,42 +17,52 @@ function fakeReducer(state: State = initialState, action: Actions): State {
1517
return state;
1618
}
1719

18-
const store = createStore(fakeReducer, applyMiddleware(thunk));
20+
const store = createStore(fakeReducer, applyMiddleware(thunk as ThunkMiddleware<State, Actions>));
1921

2022
store.dispatch(dispatch => {
2123
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'});
2229
});
2330

24-
function testGetState(): ThunkAction<void, State, {}, Actions> {
31+
function testGetState(): ThunkResult<void> {
2532
return (dispatch, getState) => {
2633
const state = getState();
2734
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());
2843
};
2944
}
3045

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'});
3152
store.dispatch(testGetState());
3253

3354
const storeThunkArg = createStore(
3455
fakeReducer,
35-
applyMiddleware(thunk.withExtraArgument('bar'))
56+
applyMiddleware(thunk.withExtraArgument('bar') as ThunkMiddleware<State, Actions, string>)
3657
);
3758

3859
storeThunkArg.dispatch((dispatch, getState, extraArg) => {
3960
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'});
4067
console.log(extraArg);
4168
});
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

Comments
 (0)