Skip to content

Commit b9d9000

Browse files
author
Wang Zhongliang
committed
Update type definition, Fix test cases
1 parent 9d5899c commit b9d9000

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

index.d.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
import { Middleware, Dispatch, Action, AnyAction } from "redux";
1+
import { Middleware, Action, AnyAction } from "redux";
22

33

44
export type ThunkAction<R, S = {}, E = {}, A extends Action<any> = AnyAction> = (
5-
dispatch: Dispatch<A>,
5+
dispatch: ThunkDispatch<S, E, A>,
66
getState: () => S,
77
extraArgument: E
88
) => R;
99

10-
declare module "redux" {
11-
export interface Dispatch<A extends Action<any> = AnyAction> {
12-
<R, E>(asyncAction: ThunkAction<R, {}, E, A>): R;
13-
}
10+
interface ThunkDispatch<S = {}, E = {}, A extends Action = AnyAction> {
11+
<T extends A>(action: T): T;
1412
}
1513

16-
declare const thunk: Middleware & {
17-
withExtraArgument(extraArgument: any): Middleware;
14+
interface ThunkDispatch<S = {}, E = {}, A extends Action = AnyAction> {
15+
<R>(asyncAction: ThunkAction<R, S, E, A>): R;
16+
}
17+
18+
type ThunkMiddleware<E, S = {}> = Middleware<ThunkDispatch<S, E>, S, ThunkDispatch<S, E>>;
19+
20+
declare const thunk: ThunkMiddleware<undefined> & {
21+
withExtraArgument<E>(extraArgument: E): ThunkMiddleware<E>
1822
};
1923

2024
export default thunk;

test/typescript.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import {Store, Middleware} from 'redux';
2-
import thunk, {ThunkAction} from '../index';
1+
import { createStore, applyMiddleware } from 'redux';
2+
import thunk, { ThunkAction } from '../index';
33

44
type State = {
5-
foo: string
5+
foo: string;
66
};
77

88
type Actions = { type: 'FOO' };
99

10-
declare const store: Store<State, Actions>;
10+
function fakeReducer(state: State, action: Actions): State {
11+
return state;
12+
}
13+
14+
const store = createStore(fakeReducer, applyMiddleware(thunk));
1115

1216
store.dispatch(dispatch => {
13-
dispatch({type: 'FOO'});
17+
dispatch({ type: 'FOO' });
1418
});
1519

1620
function testGetState(): ThunkAction<void, State, {}, Actions> {
@@ -22,20 +26,27 @@ function testGetState(): ThunkAction<void, State, {}, Actions> {
2226

2327
store.dispatch(testGetState());
2428

25-
const middleware: Middleware = thunk.withExtraArgument('bar');
29+
const storeThunkArg = createStore(
30+
fakeReducer,
31+
applyMiddleware(thunk.withExtraArgument('bar'))
32+
);
2633

27-
store.dispatch((dispatch, getState, extraArg) => {
34+
storeThunkArg.dispatch((dispatch, getState, extraArg) => {
35+
const bar: string = extraArg;
2836
console.log(extraArg);
2937
});
3038

31-
const thunkAction: ThunkAction<void, {foo: string}, {bar: number}> =
32-
(dispatch, getState, extraArg) => {
33-
const foo: string = getState().foo;
34-
const bar: number = extraArg.bar;
39+
const thunkAction: ThunkAction<void, State, { bar: number }, Actions> = (
40+
dispatch,
41+
getState,
42+
extraArg
43+
) => {
44+
const foo: string = getState().foo;
45+
const bar: number = extraArg.bar;
3546

36-
dispatch({type: 'FOO'});
37-
};
47+
dispatch({ type: 'FOO' });
48+
};
3849

39-
const thunkActionDispatchOnly: ThunkAction<void, {}, {}> = dispatch => {
40-
dispatch({type: 'FOO'});
50+
const thunkActionDispatchOnly: ThunkAction<void, {}, {}, Actions> = dispatch => {
51+
dispatch({ type: 'FOO' });
4152
};

0 commit comments

Comments
 (0)