redux - `dispatch` 方法的类型签名不正确,尽管使用了“app dispatch”钩子
问题描述
介绍
我正在使用Redux Toolkit将 Redux 支持添加到由 Django 应用程序提供的 React 应用程序。我们正在使用 Typescript,因此我们遵循 Redux Toolkit 文档中的Typescript 快速入门。
将 Redux (Toolkit?) 与 Typescript 一起使用的一个重要元素是确保您的dispatch(...)
方法具有正确的类型签名。如文档中所述,如果您只是使用useDispatch(...)
react-redux 中的钩子,
默认
Dispatch
类型不知道 thunk。为了正确调度 thunk,您需要使用AppDispatch
商店中包含 thunk 中间件类型的特定自定义类型,并将其与useDispatch
. 添加一个预先输入的useDispatch
钩子可以防止你忘记AppDispatch
在需要的地方导入。
没关系,我按照这些说明进行操作。我的代码导出了一个新的钩子,就像教程一样:
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch<AppDispatch>();
我的问题是:当我使用那个钩子时,该dispatch(...)
方法的类型签名仍然不接受 thunk。以下代码:
const dispatch = useAppDispatch();
dispatch(customAction({ ... params ... }));
产生编译错误:
ERROR in [at-loader] ./src/components/Widget.tsx:45:9
TS2345: Argument of type 'AsyncThunkAction<void, CustomActionParams, {}>' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type 'AsyncThunkAction<void, CustomActionParams, {}>' but required in type 'AnyAction'.
我的问题:为什么键入的调度方法不能正确接受AsyncThunkAction
s?
我尝试过的事情
有趣的是,使用显式参数化dispatch(...)
的AppDispatch
类型调用会导致相同的错误。只有参数化dispatch(...)
可以any
消除错误,这显然是不理想的。
const dispatch = useAppDispatch();
dispatch<AppDispatch>(customAction({ ... params ... })); // same error
dispatch<any>(customAction({ ... params ... })); // error silenced
这让我认为这个问题并AppDispatch
没有正确地推断出它应该采用AsyncThunkAction
类型,而这应该自动发生。可能导致这种情况的原因是我的异步操作是在 中定义的extraReducers
,如下所示:
export const customAction = createAsyncThunk(
"slice/customAction",
async (payload: CustomActionParams) { ... }
);
export const slice = createSlice({
name: "slice",
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(customAction.fulfilled, (state, action) => { ... });
}
});
我不确定为什么这不会导致类型更新,但这是我必须解决的问题。
与标准设置的差异
关于我的环境的一些事情在理论上也可能导致这个问题,所以为了正确起见,我将它们包括在这里:
- 这个 React 应用程序不是单页应用程序,而是使用django-react-templatetags嵌入到渲染的 Django 模板中。该库自动内联注入脚本标签以传递组件道具并使用
ReactDOM
. 有关其工作原理的示例, 请参见此页面。- 这意味着每个组件都有自己的
<Provider>
组件,而不是<Provider>
根目录,但都共享同一个存储。
- 这意味着每个组件都有自己的
- 自
useAppDispatch(...)
定义方法是在我的存储文件 (store.ts
) 中定义的,而不是hooks.ts
在根目录中定义的,但这不会导致任何问题。 - 编辑:所有这些都在Yarn 2 Workspace中,而不是包的根目录中。
不重复的问题:
- 这个问题使用了不正确的定义
useAppDispatch(...)
。我在我的代码中使用了正确的定义。 - 这个问题不使用自定义调度钩子。
- 这个问题在我目前不使用的
dispatch
方法中对thunkAPI
对象的payloadCreator
方法createAsyncThunk
使用了不正确和过度指定的类型。我正在尝试dispatch
在组件中使用通过钩子导入的方式,而不是在我的payloadCreator
方法中。
解决方案
react-redux
这个问题最终是由和@reduxjs/toolkit
包之间的某种版本不匹配引起的。我删除了我的yarn.lock
并删除了我的node_modules
,然后从头开始重新安装,问题就消失了!
~4.1.5
(此外,由于使用 Yarn 2 安装 TypeScript 的另一个问题,我还不得不将 TypeScript 固定在。)
推荐阅读
- c# - C# 读取 DWORD 块中的文件
- javascript - 从分钟和小时的字符串计算分钟
- php - How to load html file inside cakephp using dompdf
- c# - 正方体 OCR 文本位置
- python - 错误:无法复制'
.egg-info/requires.txt':不存在或不是常规文件 - javascript - 在绘制之前为(自定义)按钮设置初始状态 - HighCharts
- javascript - 单击按钮时不显示 Material-ui 菜单
- vba - 使用VBA查找一列中的值,然后将相邻列中的值相加
- javascript - Javascript array of key pair objects to JSON
- javascript - Javascript XMLHttpRequest PHP 返回“未定义”