【发布时间】:2019-03-29 09:08:24
【问题描述】:
我有一个异步的 redux 操作,所以我正在使用 thunk 中间件。
我有一个组件的mapStateToProps、mapDispatchToProps 和connect 函数如下:
const mapStateToProps = (store: IApplicationState) => {
return {
loading: store.products.loading,
products: store.products.products
};
};
const mapDispatchToProps = (dispatch: any) => {
return {
getAllProducts: () => dispatch(getAllProducts())
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(ProductsPage);
这一切都有效,但我想知道是否可以替换 mapDispatchToProps 中调度参数上的 any 类型?
我尝试了ThunkDispatch<IApplicationState, void, Action>,但在连接函数上出现以下 TypeScript 错误:
Argument of type 'typeof ProductsPage' is not assignable to parameter of type 'ComponentType<Matching<{ loading: boolean; products: IProduct[]; } & { getAllProducts: () => Promise<void>; }, IProps>>'.
Type 'typeof ProductsPage' is not assignable to type 'ComponentClass<Matching<{ loading: boolean; products: IProduct[]; } & { getAllProducts: () => Promise<void>; }, IProps>, any>'.
Types of property 'getDerivedStateFromProps' are incompatible.
Type '(props: IProps, state: IState) => { products: IProduct[]; search: string; }' is not assignable to type 'GetDerivedStateFromProps<Matching<{ loading: boolean; products: IProduct[]; } & { getAllProducts: () => Promise<void>; }, IProps>, any>'.
Types of parameters 'props' and 'nextProps' are incompatible.
Type 'Readonly<Matching<{ loading: boolean; products: IProduct[]; } & { getAllProducts: () => Promise<void>; }, IProps>>' is not assignable to type 'IProps'.
Types of property 'getAllProducts' are incompatible.
Type '() => Promise<void>' is not assignable to type '() => (dispatch: Dispatch<AnyAction>) => Promise<void>'.
Type 'Promise<void>' is not assignable to type '(dispatch: Dispatch<AnyAction>) => Promise<void>'.
Type 'Promise<void>' provides no match for the signature '(dispatch: Dispatch<AnyAction>): Promise<void>'.
是否可以替换mapDispatchToProps中dispatch参数上的any类型?
【问题讨论】:
-
getAllProductsthunk 的类型签名是什么?我有一个使用 typesafe-actions here 输入 redux 和 redux-thunk 的示例,但要看到全局可能会很棘手。查看其余相关代码可能会有所帮助。
标签: reactjs typescript redux redux-thunk