Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload for bindActionCreators #224

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Middleware, Action, AnyAction } from "redux";
import {
Action,
ActionCreatorsMapObject,
AnyAction,
Dispatch,
Middleware,
} from 'redux';

export interface ThunkDispatch<S, E, A extends Action> {
<R>(thunkAction: ThunkAction<R, S, E, A>): R;
Expand All @@ -11,10 +17,33 @@ export type ThunkAction<R, S, E, A extends Action> = (
extraArgument: E
) => R;

/**
* Takes a ThunkAction and returns a function signature which matches how it would appear when processed using
* bindActionCreators
*
* @template T ThunkAction to be wrapped
*/
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>>;

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

export default thunk;

/**
* Redux behaviour changed by middleware, so overloads here
*/
declare module 'redux' {
/**
* Overload for bindActionCreators redux function, returns expects responses
* from thunk actions
*/
function bindActionCreators<M extends ActionCreatorsMapObject<any>>(
actionCreators: M,
dispatch: Dispatch,
): { [N in keyof M]: ReturnType<M[N]> extends ThunkAction<any, any, any, any> ? (...args: Parameters<M[N]>) => ReturnType<ReturnType<M[N]>> : M[N] }
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"mocha": "^2.2.5",
"redux": "~4.0.0",
"rimraf": "^2.5.2",
"typescript": "~2.6.2",
"typescript": "^3.1.6",
"typings-tester": "^0.3.1",
"webpack": "^1.12.14"
}
Expand Down
54 changes: 50 additions & 4 deletions test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createStore, applyMiddleware } from 'redux';
import thunk, { ThunkAction, ThunkMiddleware, ThunkDispatch } from '../index';
import {
applyMiddleware,
bindActionCreators,
createStore,
} from 'redux';

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

type State = {
foo: string;
Expand Down Expand Up @@ -50,6 +60,42 @@ function anotherThunkAction(): ThunkResult<string> {
}
}

function promiseThunkAction(): ThunkResult<Promise<boolean>> {
return async (dispatch, getState) => {
dispatch({ type: 'FOO' });
return false;
}
}

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: ActionDispatchs = bindActionCreators({
anotherThunkAction,
promiseThunkAction,
standardAction,
}, store.dispatch);



actions.anotherThunkAction() === 'hello';
// typings:expect-error
actions.anotherThunkAction() === false;
actions.promiseThunkAction().then((res) => console.log(res));
// typings:expect-error
actions.promiseThunkAction().prop;
actions.standardAction().type;
// typings:expect-error
actions.standardAction().other;


store.dispatch({ type: 'FOO' });
// typings:expect-error
store.dispatch({ type: 'BAR' })
Expand Down Expand Up @@ -85,5 +131,5 @@ const callDispatchAsync_specificActions = (dispatch: ThunkDispatch<State, undefi
const callDispatchAny = (dispatch: ThunkDispatch<State, undefined, Actions>) => {
const asyncThunk = (): any => () => ({} as Promise<void>);
dispatch(asyncThunk()) // result is any
.then(() => console.log('done'))
}
.then(() => console.log('done'))
}