From f86bd3f343a420e880590e74c1f139f197fb9a55 Mon Sep 17 00:00:00 2001 From: Wang Zhongliang Date: Tue, 6 Mar 2018 15:09:09 +0800 Subject: [PATCH] Fix typings and test cases --- index.d.ts | 8 +++---- test/index.js | 4 ++-- test/typescript.ts | 52 ++++++++++++++++++++++++++++------------------ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/index.d.ts b/index.d.ts index f27d37e..8704c9e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,10 +11,10 @@ export type ThunkAction = ( extraArgument: E ) => R; -type ThunkMiddleware = Middleware, S, ThunkDispatch>; +export type ThunkMiddleware = Middleware, S, ThunkDispatch>; -declare const thunk: ThunkMiddleware & { - withExtraArgument(extraArgument: E): ThunkMiddleware -}; +declare const thunk: ThunkMiddleware & { + withExtraArgument(extraArgument: E): ThunkMiddleware<{}, AnyAction, E> +} export default thunk; diff --git a/test/index.js b/test/index.js index b9099fe..10d1d49 100644 --- a/test/index.js +++ b/test/index.js @@ -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', () => { @@ -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); }); }); }); diff --git a/test/typescript.ts b/test/typescript.ts index 4c5e75b..ecf67f4 100644 --- a/test/typescript.ts +++ b/test/typescript.ts @@ -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 = ThunkAction; const initialState: State = { foo: 'foo' @@ -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)); 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 { +function testGetState(): ThunkResult { 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) ); 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 = ( - dispatch, - getState, - extraArg -) => { - const foo: string = getState().foo; - const bar: number = extraArg.bar; - - dispatch({ type: 'FOO' }); -}; - -const thunkActionDispatchOnly: ThunkAction = dispatch => { - dispatch({ type: 'FOO' }); -};