0

I have a Thunk Action that looks like:

export const doTestCall = (teamId:string): ThunkAction<()=>void, RootState, null, MoodAction> => {
    return dispatch => {
      const unsubscribe = db.doc("id").onSnapshot(doc =>{
        //whatever
      });
      return unsubscribe;
    }
  }

In my react component I have the following hook:

useEffect(() => {
        const unsubscribe = dispatch(doSomething()) as ()=>void;
        return () => unsubscribe();
}, []);

This code actually works. Question I have, how can I remove forcing the

as ()=>void 

If I do, code complains in the "return line" with:

const unsubscribe: (dispatch: ThunkDispatch<RootState, null, MoodAction>, getState: () => RootState, extraArgument: null) => void
Expected 3 arguments, but got 0.ts(2554)
index.d.ts(9, 3): An argument for 'dispatch' was not provided.

Thanks,

ramon_salla
  • 1,559
  • 1
  • 16
  • 34

1 Answers1

1

It seems that ThunkAction<()=>void, RootState, null, MoodAction> does not match the definition of your middleware.

I would assume that something like

ThunkAction<()=>void, RootState, unknown, AnyAction>
//or
ThunkAction<()=>void, RootState, any, AnyAction>

would work better, assuming your dispatch typings include the types for the thunk middleware at all.

If you are using the officially recommended redux toolkit, setting up the correct dispatch type is documented here: https://redux-toolkit.js.org/tutorials/typescript

phry
  • 4,685
  • 1
  • 10
  • 21