Skip to content

Commit

Permalink
Fix typings and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang Zhongliang committed Mar 6, 2018
1 parent 196df61 commit f86bd3f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export type ThunkAction<R, S, E, A extends Action> = (
extraArgument: E
) => R;

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

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

export default thunk;
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chai from 'chai';
import thunkMiddleware from '../src/index';
import { check } from 'typings-tester';
import { checkDirectory } from 'typings-tester';


describe('thunk middleware', () => {
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('thunk middleware', () => {
this.timeout(0);

it('should compile against index.d.ts', () => {
check([__dirname + '/typescript.ts'], __dirname + '/tsconfig.json');
checkDirectory(__dirname);
});
});
});
52 changes: 32 additions & 20 deletions test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createStore, applyMiddleware } from 'redux';
import thunk, { ThunkAction } from '../index';
import thunk, { ThunkAction, ThunkMiddleware } from '../index';

type State = {
foo: string;
};

type Actions = { type: 'FOO' };
type Actions = { type: 'FOO' } | { type: 'BAR', result: number };

type ThunkResult<R> = ThunkAction<R, State, undefined, Actions>;

const initialState: State = {
foo: 'foo'
Expand All @@ -15,42 +17,52 @@ function fakeReducer(state: State = initialState, action: Actions): State {
return state;
}

const store = createStore(fakeReducer, applyMiddleware(thunk));
const store = createStore(fakeReducer, applyMiddleware(thunk as ThunkMiddleware<State, Actions>));

store.dispatch(dispatch => {
dispatch({ type: 'FOO' });
// typings:expect-error
dispatch({ type: 'BAR' })
dispatch({ type: 'BAR', result: 5 })
// typings:expect-error
store.dispatch({ type: 'BAZ'});
});

function testGetState(): ThunkAction<void, State, {}, Actions> {
function testGetState(): ThunkResult<void> {
return (dispatch, getState) => {
const state = getState();
const foo: string = state.foo;
dispatch({ type: 'FOO' });
// typings:expect-error
dispatch({ type: 'BAR'});
dispatch({ type: 'BAR', result: 5 });
// typings:expect-error
dispatch({ type: 'BAZ'});
// Can dispatch another thunk action
dispatch(testGetState());
};
}

store.dispatch({ type: 'FOO' });
// typings:expect-error
store.dispatch({ type: 'BAR' })
store.dispatch({ type: 'BAR', result: 5 })
// typings:expect-error
store.dispatch({ type: 'BAZ'});
store.dispatch(testGetState());

const storeThunkArg = createStore(
fakeReducer,
applyMiddleware(thunk.withExtraArgument('bar'))
applyMiddleware(thunk.withExtraArgument('bar') as ThunkMiddleware<State, Actions, string>)
);

storeThunkArg.dispatch((dispatch, getState, extraArg) => {
const bar: string = extraArg;
store.dispatch({ type: 'FOO' });
// typings:expect-error
store.dispatch({ type: 'BAR' })
store.dispatch({ type: 'BAR', result: 5 })
// typings:expect-error
store.dispatch({ type: 'BAZ'});
console.log(extraArg);
});

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

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

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

0 comments on commit f86bd3f

Please sign in to comment.