Is there any way to correctly dispatch a Thunk from a store created with redux-mock-store ? Right now I am forced to use any type assertion
store.dispatch<any>(getCommissions());
As dispatch
expects to provide plain Action
[ts]
Argument of type '(dispatch: ThunkDispatch<IRootState, undefined,
AnyAction>, getState: () => IRootState) => void' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type '(dispatch: ThunkDispatch<IRootState, undefined, AnyAction>, getState: () => IRootState) => void'.
Code fragment for getCommisions()
export function getCommissions() {
return (dispatch: ThunkDispatch<IRootState, undefined, AnyAction>, getState: () => IRootState) => { ... }
The createMockStore
function that is the default export of redux-mock-store
accepts generic types <S, DispatchExts>
where S
is the definition of your Redux state, and DispatchExts
is a union of (or single) additional Dispatch
signatures for any added Redux middleware.
So the way to set this up is to import ThunkDispatch
from redux-thunk
, which accepts its own generic arguments of <State, ExtraArgument, Action>
and pass that in as the DispatchExts
argument to createMockStore
.
Here's an abbreviated example:
import { AnyAction } from 'redux'; // Or your own Action definition
import createMockStore from 'redux-mock-store';
import thunk, { ThunkDispatch } from 'redux-thunk';
import { ApplicationState } from '../your/definitions';
type DispatchExts = ThunkDispatch<ApplicationState, void, AnyAction>;
const middleware = [thunk];
const mockStore = createMockStore<ApplicationState, DispatchExts>(middleware);
const store = mockStore();
Hope that helps you!
My current versions:
redux 4.0.0
redux-thunk 2.3.0
redux-mock-store 1.5.3
typescript 2.9.2
ThunkDispatch
defintion https://github.com/reduxjs/redux-thunk/blob/master/index.d.ts
createMockStore
definition https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/redux-mock-store/index.d.ts
For configureMockStore
use, the solution is almost exactly the same as the answer by @nickpassarella.
import configureMockStore from 'redux-mock-store';
import thunk, { ThunkDispatch } from 'redux-thunk';
import { MyState, MyActions } from './my-types';
type DispatchExts = ThunkDispatch<MyState, undefined, MyActions>;
const middlewares = [thunk];
const mockStore = configureMockStore<MyState, DispatchExts>(middlewares);
const store = mockStore();
Both examples working with redux 4.0.1
redux-thunk 2.3.0
redux-mock-store 1.5.3
and typescript 3.5.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With