属性 类型不存在 - redux-thunk 等待分派
Property does not exist on type - redux-thunk await dispatch
我确实遇到了此处描述的这个问题 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 | }}
根据 the createAsyncThunk
docs,createAsyncThunk
不 return 实际的“响应”。相反,它 return 是“已分派的最终操作对象”,以避免代码中出现“未捕获的拒绝承诺”错误。
您可以unwrap the result action在组件中取回实际数据。
此外,请确保 you are using a Dispatch
type that correctly understands thunks。
我确实遇到了此处描述的这个问题 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 | }}
根据 the createAsyncThunk
docs,createAsyncThunk
不 return 实际的“响应”。相反,它 return 是“已分派的最终操作对象”,以避免代码中出现“未捕获的拒绝承诺”错误。
您可以unwrap the result action在组件中取回实际数据。
此外,请确保 you are using a Dispatch
type that correctly understands thunks。