You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the Redux Docs > Usage With Typescript > Type Checking Redux Thunks, it recommends using the following to type thunks:
exporttypeAppThunk<ReturnType=void>=ThunkAction<ReturnType,// return type of the thunk functionRootState,// state type used by getStateunknown,// any "extra argument" injected into the thunkUnknownAction// known types of actions that can be dispatched>
I'm using both of these in my apps, but when I try to dispatch a thunk created using createAppAsyncThunk from a regular thunk, it's flagging a type error.
What is the problem?
From what I can see, it seems the two approaches type dispatch differently. The first approach (with AppThunk) types the dispatch parameter as ThunkDispatch<RootState, unknown, UnknownAction> but the second approach (with createAsyncThunk.withTypes) types the dispatch parameter as ThunkDispatch<RootState, undefined, UnknownAction> which is causing a problem,
What should be changed to fix the problem?
I was able to fix the problem like this:
exporttypeAppThunk<ReturnType=void>=ThunkAction<ReturnType,RootState,undefined,// changed this from unknown to undefinedUnknownAction>
The text was updated successfully, but these errors were encountered:
are you specifically providing extra: undefined as one of the keys in your ThunkApiConfig for .withTypes? if you don't provide it, it should default to unknown.
What docs page needs to be fixed?
In the Redux Docs > Usage With Typescript > Type Checking Redux Thunks, it recommends using the following to type thunks:
In the RTK Docs > Usage with Typescript > Defining a Pre-Typed createAsyncThunk, it recommends the following to type
createAsyncThunk
:I'm using both of these in my apps, but when I try to dispatch a thunk created using
createAppAsyncThunk
from a regular thunk, it's flagging a type error.What is the problem?
From what I can see, it seems the two approaches type
dispatch
differently. The first approach (withAppThunk
) types thedispatch
parameter asThunkDispatch<RootState, unknown, UnknownAction>
but the second approach (withcreateAsyncThunk.withTypes
) types thedispatch
parameter asThunkDispatch<RootState, undefined, UnknownAction>
which is causing a problem,What should be changed to fix the problem?
I was able to fix the problem like this:
The text was updated successfully, but these errors were encountered: