I have set up a redux thunk function with typescript like this:
export const fetchActivities = (): AppThunk => async dispatch => {
dispatch(fetchActivitiesRequest())
try {
const res = await fetch("/activities")
const body = await res.json()
dispatch(fetchActivitiesSuccess(body))
} catch (err) {
dispatch(fetchActivitiesError(err))
}
}
AppThunk is simply a type deriving from the ThunkAction type, with the following type params:
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, {}, null, Action<string>>
My issue is, that when i try to setup a unit test for my action, and when I try to dispatch the thunk action like this:
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const store = mockStore({ activities: [] })
store.dispatch(actions.fetchActivities())
I receive the following error message:
Argument of type 'ThunkAction<void, {}, null, Action<string>>' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type 'ThunkAction<void, {}, null, Action<string>>' but required in type 'AnyAction'.ts(2345)
I have tried searching around for a solution to this issue, though without success. Most suggestions say to try and add an any param to the generic for dispatch, so dispatch<any>(actions.fetchActivities())
. While this works to stop the typing error, only the first action in the thunk will get dispatched, while everything within the try and catch statements are ignored.
Does anyone have any suggestions as to how to fix this issue?