Skip to content

Commit

Permalink
Fix ThunkActionDispatch: generic should expect to extend a function, …
Browse files Browse the repository at this point in the history
…not just the return type
  • Loading branch information
RMHonor committed Dec 3, 2018
1 parent 47baac3 commit 17bb88a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ThunkAction<R, S, E, A extends Action> = (
*
* @template T ThunkAction to be wrapped
*/
export type ThunkActionDispatch<T extends ThunkAction<any, any, any, any>> = (...args: Parameters<T>)
export type ThunkActionDispatch<T extends (...args: any[]) => ThunkAction<any, any, any, any>> = (...args: Parameters<T>)
=> ReturnType<ReturnType<T>>;

export type ThunkMiddleware<S = {}, A extends Action = AnyAction, E = undefined> = Middleware<ThunkDispatch<S, E, A>, S, ThunkDispatch<S, E, A>>;
Expand Down
11 changes: 10 additions & 1 deletion test/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {

import thunk, {
ThunkAction,
ThunkActionDispatch,
ThunkMiddleware,
} from '../index';

Expand Down Expand Up @@ -67,14 +68,22 @@ function promiseThunkAction(): ThunkResult<Promise<boolean>> {

const standardAction = () => ({ type: 'FOO' });

interface ActionDispatchs {
anotherThunkAction: ThunkActionDispatch<typeof anotherThunkAction>;
promiseThunkAction: ThunkActionDispatch<typeof promiseThunkAction>;
standardAction: typeof standardAction;
}

// test that bindActionCreators correctly returns actions responses of ThunkActions
// also ensure standard action creators still work as expected
const actions = bindActionCreators({
const actions: ActionDispatchs = bindActionCreators({
anotherThunkAction,
promiseThunkAction,
standardAction,
}, store.dispatch);



actions.anotherThunkAction() === 'hello';
// typings:expect-error
actions.anotherThunkAction() === false;
Expand Down

0 comments on commit 17bb88a

Please sign in to comment.