Skip to content

Commit

Permalink
wip: feat(ts): add Dispatch overload to redux module (#278)
Browse files Browse the repository at this point in the history
It seems #259 author is not active anymore

Closes #259
  • Loading branch information
iamandrewluca committed Feb 10, 2020
1 parent 9ce44a8 commit d28ab03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/index.d.ts
Expand Up @@ -122,4 +122,14 @@ declare module 'redux' {
) => ReturnType<ReturnType<TActionCreators[TActionCreatorName]>>
: TActionCreators[TActionCreatorName];
};

/*
* Overload to add thunk support to Redux's dispatch() function.
* Useful for react-redux or any other library which could use this type.
*/
export interface Dispatch<A extends Action = AnyAction> {
<TReturnType = any, TState = any, TExtraThunkArg = any>(
thunkAction: ThunkAction<TReturnType, TState, TExtraThunkArg, A>,
): TReturnType;
}
}
12 changes: 11 additions & 1 deletion test/typescript.ts
@@ -1,4 +1,9 @@
import { applyMiddleware, bindActionCreators, createStore } from 'redux';
import {
applyMiddleware,
bindActionCreators,
createStore,
Dispatch,
} from 'redux';

import thunk, {
ThunkAction,
Expand Down Expand Up @@ -144,3 +149,8 @@ const callDispatchAny = (
dispatch(asyncThunk()) // result is any
.then(() => console.log('done'));
};

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

untypedStore.dispatch(anotherThunkAction());
untypedStore.dispatch(promiseThunkAction()).then(() => Promise.resolve());

0 comments on commit d28ab03

Please sign in to comment.