【发布时间】:2020-04-24 23:20:26
【问题描述】:
我有两个组件的示例 Tasks 和 Task 在父组件 Tasks 我已经连接了 store
type Props = ConnectedProps<typeof connector>
const mapDispatchToProps = { updateTask };
const connector = connect(mapStateToProps, mapDispatchToProps);
export default connector(Tasks)
然后在子 Task 我想使用 updateTask 因为我不想将每个任务都连接到 redux
任务看起来像
type Props = {
updateTask: typeof updateTask
}
但该代码导致
类型 '(taskId: string, updatedTask: Partial) => void' 不可分配给类型 '(taskId: string, updatedTask: Partial) => ThunkAction'。类型“void”不可分配给类型“ThunkAction”。
当然,因为它不通过 redux 连接
所以我的问题是使用“react-redux”库中的InferThunkActionCreatorType 是否安全?
type Props = {
updateTask: InferThunkActionCreatorType<typeof updateTaskAndPropagate>
}
定义是
export type InferThunkActionCreatorType<TActionCreator extends (...args: any[]) => any> =
TActionCreator extends (...args: infer TParams) => (...args: any[]) => infer TReturn
? (...args: TParams) => TReturn
: TActionCreator;
这正是我需要的相同函数参数,但返回类型为 void
【问题讨论】:
标签: reactjs typescript redux redux-thunk