AsyncThunkAction不适合AnyAction吗?

问题描述 投票:0回答:1

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

我使用了建议https://react-redux.js.org/using-react-redux/static-typing#typing-the-usedispatch-hook的AppDispatch和此问题How do I resolve 'Property 'type' is missing in type 'AsyncThunkAction' using Redux Toolkit (with TypeScript)?但是可移植性很强。

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)
redux redux-thunk redux-toolkit
1个回答
0
投票

我必须对createAsyncThunk函数中的thunkAPI参数类型使用RootState

  {
    dispatch: AppDispatch
    state: RootState
    rejectValue: MyKnownError
  }
© www.soinside.com 2019 - 2024. All rights reserved.