reactjs - 带有 TypeScript 的 Redux Toolkit:如何正确键入异步 thunk 的返回值
问题描述
我有这样的异步重击
export const updateIndexingConfig = createAsyncThunk(
"settings/updateIndexingConfig",
(config: UpdateIndexingConfigurationRequest) => {
return sdkClient.updateIndexingConfiguration(config).promise();
}
);
更新请求可以返回错误消息(如果有)。将updateIndexingConfiguration.promise()
返回一个承诺。当用户点击这个按钮时它被调度
<Button
onClick={async () => {
if (!isFormValid()) return;
const updateIndexingConfigResponse = await dispatch(
updateIndexingConfig(constructUpdateIndexingConfigRequest(indexingConfig))
);
if(updateIndexingConfigResponse.error) {
// ... show error banner
} else {
window.location.href = "#/settings/";
}
}}
>
Update
</Button>;
TypeScript 编译器在这一行给我这个错误if(updateIndexingConfigResponse.error)
说
类型“AsyncThunkAction<any, UpdateIndexingConfigurationRequest, {}>”上不存在属性“错误”。
我认为这是由于 SDK 没有定义响应类型。但是通过阅读它的文档,我知道当出现问题时响应可能会出现错误消息。所以我很快自己做了一个
interface UpdateIndexingConfigurationResponse {
error?: {
message: string;
};
[key: string]: any;
}
我在 thunk 中添加了它
export const updateIndexingConfig = createAsyncThunk(
"settings/updateIndexingConfig",
(config: UpdateIndexingConfigurationRequest): UpdateIndexingConfigurationResponse => {
return sdkClient.updateIndexingConfiguration(config).promise();
}
);
error
但是,当我访问调度返回的响应时,编译器仍然对此不满意。
类型“AsyncThunkAction<UpdateIndexingConfigurationResponse, UpdateIndexingConfigurationRequest, {}>”上不存在属性“错误”。
不知道我在这里缺少什么。在这种情况下,如何正确输入响应?
也是一个小问题,TS 编译器也对我大喊大叫await dispatch(updateIndexingConfig(...))
,说
'await' 对此表达式的类型没有影响。ts(80007)
但我知道它确实有效果。await
在这种情况下确实如此
顺便说一句,请不要质疑代码是否有效,或者即使sdkClient.updateIndexingConfiguration(config).promise()
返回一个承诺,我测试它并且我知道它有效。只是缺少打字,这是我的问题。
解决方案
您dispatch
没有考虑 thunk 的类型,因此返回类型输入错误。请使用文档Dispatch
中描述的商店中的实际类型:
import { configureStore } from '@reduxjs/toolkit'
import { useDispatch } from 'react-redux'
import rootReducer from './rootReducer'
const store = configureStore({
reducer: rootReducer
})
export type AppDispatch = typeof store.dispatch
export const useAppDispatch = () => useDispatch<AppDispatch>() // Export a hook that can be reused to resolve types
推荐阅读
- javascript - 如何使用 Javascript 或 jquery 从多个复选框中仅选择一个复选框?
- c# - MongoDB 4.4.4 更改流返回所有文档更新而不是过滤条件匹配一个?
- python - 使用不同的参数多次运行 python 脚本
- sql - postgres 函数:传递和遍历数组
- javascript - React Native Expo 音频会导致应用程序在默认示例中冻结?
- javascript - 如何在 Next JS 中使用代理进行 rest api 调用
- java - 反应式应用程序中的 Spring Cache
- firebase - Firebase 存储占用异常高的带宽和请求
- swift - 如何创建一个带有属性字符串的 pdf,其中包含一个编码字符串(Swift)?
- java - 单机搭建kafka集群及配置