Skip to content

Commit

Permalink
Update type definition, Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang Zhongliang committed Feb 27, 2018
1 parent 9d5899c commit b9d9000
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
20 changes: 12 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { Middleware, Dispatch, Action, AnyAction } from "redux";
import { Middleware, Action, AnyAction } from "redux";


export type ThunkAction<R, S = {}, E = {}, A extends Action<any> = AnyAction> = (
dispatch: Dispatch<A>,
dispatch: ThunkDispatch<S, E, A>,
getState: () => S,
extraArgument: E
) => R;

declare module "redux" {
export interface Dispatch<A extends Action<any> = AnyAction> {
<R, E>(asyncAction: ThunkAction<R, {}, E, A>): R;
}
interface ThunkDispatch<S = {}, E = {}, A extends Action = AnyAction> {
<T extends A>(action: T): T;
}

declare const thunk: Middleware & {
withExtraArgument(extraArgument: any): Middleware;
interface ThunkDispatch<S = {}, E = {}, A extends Action = AnyAction> {
<R>(asyncAction: ThunkAction<R, S, E, A>): R;
}

type ThunkMiddleware<E, S = {}> = Middleware<ThunkDispatch<S, E>, S, ThunkDispatch<S, E>>;

declare const thunk: ThunkMiddleware<undefined> & {
withExtraArgument<E>(extraArgument: E): ThunkMiddleware<E>
};

export default thunk;
41 changes: 26 additions & 15 deletions test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import {Store, Middleware} from 'redux';
import thunk, {ThunkAction} from '../index';
import { createStore, applyMiddleware } from 'redux';
import thunk, { ThunkAction } from '../index';

type State = {
foo: string
foo: string;
};

type Actions = { type: 'FOO' };

declare const store: Store<State, Actions>;
function fakeReducer(state: State, action: Actions): State {
return state;
}

const store = createStore(fakeReducer, applyMiddleware(thunk));

store.dispatch(dispatch => {
dispatch({type: 'FOO'});
dispatch({ type: 'FOO' });
});

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

store.dispatch(testGetState());

const middleware: Middleware = thunk.withExtraArgument('bar');
const storeThunkArg = createStore(
fakeReducer,
applyMiddleware(thunk.withExtraArgument('bar'))
);

store.dispatch((dispatch, getState, extraArg) => {
storeThunkArg.dispatch((dispatch, getState, extraArg) => {
const bar: string = extraArg;
console.log(extraArg);
});

const thunkAction: ThunkAction<void, {foo: string}, {bar: number}> =
(dispatch, getState, extraArg) => {
const foo: string = getState().foo;
const bar: number = extraArg.bar;
const thunkAction: ThunkAction<void, State, { bar: number }, Actions> = (
dispatch,
getState,
extraArg
) => {
const foo: string = getState().foo;
const bar: number = extraArg.bar;

dispatch({type: 'FOO'});
};
dispatch({ type: 'FOO' });
};

const thunkActionDispatchOnly: ThunkAction<void, {}, {}> = dispatch => {
dispatch({type: 'FOO'});
const thunkActionDispatchOnly: ThunkAction<void, {}, {}, Actions> = dispatch => {
dispatch({ type: 'FOO' });
};

0 comments on commit b9d9000

Please sign in to comment.