0

codesandbox is here https://codesandbox.io/s/rtk-github-issues-example-03-final-async-su4hz?file=/src/features/issuesList/IssuesListPage.tsx

I've used AppDispatch which suggested https://react-redux.js.org/using-react-redux/static-typing#typing-the-usedispatch-hook and this question How do I resolve 'Property 'type' is missing in type 'AsyncThunkAction' using Redux Toolkit (with TypeScript)? but transpile is faield.

No overload matches this call.
  Overload 1 of 3, '(action: AnyAction): AnyAction', gave the following error.
    Argument of type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' is not assignable to parameter of type 'AnyAction'.
      Property 'type' is missing in type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' but required in type 'AnyAction'.
  Overload 2 of 3, '(asyncAction: ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, null, AnyAction>): Promise<...> & { ...; }', gave the following error.
    Argument of type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' is not assignable to parameter of type 'ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, null, AnyAction>'.
      Types of parameters 'getState' and 'getState' are incompatible.
        Type 'CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>' is missing the following properties from type 'RepoDetailsState': openIssuesCount, error
  Overload 3 of 3, '(asyncAction: ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, undefined, AnyAction>): Promise<...> & { ...; }', gave the following error.
    Argument of type 'AsyncThunkAction<RepoDetails, { org: string; repo: string; }, { dispatch: ThunkDispatch<CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>, null, AnyAction> & ThunkDispatch<...> & Dispatch<...>; state: RepoDetailsState; rejectValue: MyK...' is not assignable to parameter of type 
ThunkAction<Promise<PayloadAction<RepoDetails, string, { arg: { org: string; repo: string; }; requestId: string; }, never> | PayloadAction<MyKnownError | undefined, string, { ...; }, SerializedError>> & { ...; }, CombinedState<...>, undefined, AnyAction>'.
      Types of parameters 'getState' and 'getState' are incompatible.
        Type 'CombinedState<{ issuesDisplay: CurrentDisplayState; repoDetails: RepoDetailsState; issues: IssuesState; comments: CommentsState; }>' is not assignable to type 'RepoDetailsState'.ts(2769)

2 Answers2

2

I had to use RootState for thunkAPI parameter types in createAsyncThunk function.

  {
    dispatch: AppDispatch
    state: RootState
    rejectValue: MyKnownError
  }
1

In my case I got this error because I used the wrong type for dispatch. I used AppDispatch for the thunkAPI param types, but where I tried to use the thunk I explicitly declared the wrong dispatch type. The reverse is surely also possible. Read here about AppDispatch: https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type

Using the right type for RootState like Hiroyuki Shirakawa said is a good thing to look for too.

Read here about createAsyncThunk in TypeScript: https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk

Alexander Taylor
  • 16,574
  • 14
  • 62
  • 83