6

I'm using redux-toolkit, and trying to dispatch a thunk. The dispatch function I'm using looks like this, this is directly taken from the docs. eslint complains about the missing return type, any idea what it should be?

export const useAppDispatch = () => useDispatch<AppDispatch>();

Furthermore I get the same error, as in this question How to dispatch an Action or a ThunkAction (in TypeScript, with redux-thunk)?, but there's no real solution there as far as I understood it.

So if I got code like this:

export const fetchSomeThing = createAsyncThunk<SomeType[]>('someThing/fetch', async () => {
  const someClient = useSomeClient();
  const someThing: SomeType[] = await someClient.listSomething();
  return someThing;
});

// I also tried typing dispatch as AppDispatch here explicitly, but it gives me the same error
const dispatch = useAppDispatch();
dispatch(fetchSomeThing()) //This gives an error: Property 'type' is missing in type 'AsyncThunkAction<SomeType[], void, {}>' 
// but required in type 'AnyAction'.ts(2345), index.d.ts(21, 3): 'type' is declared here.

My store looks like this:

export const store = configureStore({
  reducer: {
    someThing: someThingReducer,
  },
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch; // According to vs-code this is of type AppDispatch = Dispatch<AnyAction>
supermar10
  • 145
  • 1
  • 9

1 Answers1

7

This recently seems to happen when people have redux@4.0.5 and redux@4.1.0 installed in some way side-by-side and can usually be resolved by re-installing @types/react-redux by doing npm i @types/react-redux or yarn add @types/react-redux. Could you please try that?

phry
  • 35,762
  • 5
  • 67
  • 81
  • 1
    Ah that maybe explains why it now works after doing a yarn upgrade of all dependencies, I guess that caused a reinstall, pruning the old stuff away. Because nothing of relevance was updated – supermar10 May 14 '21 at 09:50
  • 1
    I've upgraded the packages for the solution, tried adding/removing react-redux/type dependencies from my packages file but still was experiencing the same problem. Had "@types/react-redux": "^7.1.16" installed as devDependency. Nothing helped. My dispatcher was still recognized as Dispatch by visual studio and couldn't run my code. Finally figured out that it had to do something with yarn. I had been running yarn version 2.4.1 (berry). Once I've downgraded back to 1.22.10 problem went away. Any idea why that is? – Aleksas May 25 '21 at 23:17
  • No, but I'm glad you solved it. Then using yarn, you can use the `resolutions` field in the package.json to prevent duplicates. You might want to look into that. – phry May 26 '21 at 07:23
  • 2
    @Aleksas I had the same issue using Yarn 2. What ended up working for me was explicitly adding `redux` as a dependency and making sure `@reduxjs/toolkit` was up-to-date. If you're using workspaces, the only way I was able to get it to type correctly was adding `redux` to the _root_. Some more details here: https://github.com/yarnpkg/berry/issues/3150 – Joe Krill Jul 21 '21 at 16:50
  • @JoeKrill You saved my day. For me I'm using npm v6.14.8 and once I explicitly install `redux` as a dependency, TypeScript no longer complains. – Yihao Gao Jul 22 '21 at 06:49
  • @YihaoGao I think the preferred method is to use `packageExtensions` -- but that may not even be necessary soon (next yarn release, I'm guessing?) because I believe they updated the default extensions list to include this exception. – Joe Krill Jul 22 '21 at 23:23
  • Wow, if I could upvote this more than once I would. Spent hours Googling, and installing `redux` explicitly alongside `@reduxjs/toolkit` is what solved it. Thanks everyone! – Donut Sep 22 '21 at 01:54