【问题标题】:Property does not exist on type - redux-thunk await dispatch类型上不存在属性 - redux-thunk 等待调度
【发布时间】:2021-06-26 17:43:57
【问题描述】:

我在https://github.com/reduxjs/redux-toolkit/issues/485#issuecomment-610654378 处描述了这个确切的问题

所以我直接导入了 ThunkDispatch 并使用它。如果不抛出 property does not exist 错误,我无法从调度响应中获取任何密钥

@reduxjs/toolkit@1.5.1 
const response = await dispatch(deactivateSubscription(args))
const response: PayloadAction<ApiResponse<EmptyBodyResponse>, string, {
    arg: DeactivateSubscriptionArgs;
    requestId: string;
    requestStatus: "fulfilled";
}, never> | PayloadAction<...>
export interface ApiResponse<T = unknown> {
  body: T
  error: Error
  success: boolean
  message?: string
}
TS2339: Property 'error' does not exist on type 'PayloadAction<ApiResponse<EmptyBodyResponse>, string, { arg: DeactivateSubscriptionArgs; requestId: string; requestStatus: "fulfilled"; }, never> | PayloadAction<...>'.
  Property 'error' does not exist on type 'PayloadAction<ApiResponse<EmptyBodyResponse>, string, { arg: DeactivateSubscriptionArgs; requestId: string; requestStatus: "fulfilled"; }, never>'.
    149 |
  > 150 |                     if (!response.error) {
        |                                   ^^^^^
    151 |                       setIsEditing(false)
    152 |                     }
    153 |                   }}

【问题讨论】:

    标签: typescript redux redux-thunk redux-toolkit


    【解决方案1】:

    根据the createAsyncThunk docscreateAsyncThunk 不返回实际的“响应”。相反,它返回“已调度的最终操作对象”,以避免代码中出现“未捕获的被拒绝的 Promise”错误。

    您可以unwrap the result action 获取组件中的实际数据。

    另外,请确保you are using a Dispatch type that correctly understands thunks

    【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2021-04-20
    • 2018-09-28
    • 2021-08-04
    • 2017-04-03
    • 2019-12-04
    • 2020-02-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多