Calling dispatch
with a thunk from createAsyncAppThunk
causes a Typescript error
#4312
-
Here is a link to the PR where this happens: mastodon/mastodon#29789
export const createAppAsyncThunk = createAsyncThunk.withTypes<{
state: RootState;
dispatch: AppDispatch;
rejectValue: string;
}>(); The failure is in export const importFetchedPoll = createAppAsyncThunk(
'poll/importFetched',
() => {
// …
},
);
export const fetchPoll = createAppAsyncThunk(
'poll/fetch',
async (_, { dispatch, getState }) => {
await dispatch(importFetchedPoll());
},
); That fails with
If I comment I double checked our Note: this is a legacy app that I am working on modernising, it uses Immutable.js for the root state (I am replacing the reducers incrementally to switch to Immer). I do not think it matters but this is the biggest divergence with a normal RTK app. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
the issue is with your other middleware types - they should all be The first type parameter is for adding overloads to dispatch, for example if you had a middleware that intercepted dispatched promises: const boxPromiseResultMiddleware: Middleware<
<T>(promise: Promise<T>) => Promise<{ value: T }>
> = (api) => (next) => (action) => {
if (action instanceof Promise) {
return action.then((value) => ({ value }));
}
return next(action);
};
const { value } = await dispatch(aPromise) if your middleware doesn't add any overloads, it should just use |
Beta Was this translation helpful? Give feedback.
the issue is with your other middleware types - they should all be
Middleware<{}, RootState>
. usingRecord<string, never>
is causing the issue, just disable the lint rule for those lines.same goes for the
loadingBarMiddleware
, it doesn't add any overloads to dispatch so should just be{}
for theDispatchExt
.The first type parameter is for adding overloads to dispatch, for example if you had a middleware that intercepted dispatched promises: