反应,Redux的connect()和错误TS2345
问题描述:
我无法弄清楚为什么这个代码构建失败,出现以下错误:反应,Redux的connect()和错误TS2345
(43,3): error TS2345: Argument of type 'typeof Day' is not assignable to parameter of type 'Component<Props | (Props & ConnectProps & DispatchProps)>'.
Type 'typeof Day' is not assignable to type 'StatelessComponent<Props | (Props & ConnectProps & DispatchProps)>'.
Type 'typeof Day' provides no match for the signature '(props: (Props & { children?: ReactNode; }) | (Props & ConnectProps & DispatchProps & { children?: ReactNode; }), context?: any): ReactElement<any>'.
我正在打字稿2.3.3和@类型/ react-redux的版本是4.4.41。
import * as React from "react";
import { connect } from "react-redux";
export interface Props {
date: Date;
}
export interface ConnectProps {
shifts: string[];
}
export interface DispatchProps {}
class Day extends React.Component<Props & ConnectProps & DispatchProps, null> {
render() {
/* simplified */
return <div />;
}
}
const mapStateToProps = (state: any, ownProps?: Props): ConnectProps => {
/* simplified */
return {
shifts: []
};
};
const mapDispatchToProps = (dispatch: any): DispatchProps => ({});
export default connect<ConnectProps, DispatchProps, Props>(
mapStateToProps,
mapDispatchToProps
)(Day);
let component = <Day date={new Date()} />
我想避免装饰者,只要他们是实验性的。
答
I want to avoid decorators as long as they are experimental.
错误的方法来处理它。他们工作就像JSX工作。当前的类型定义假定你将使用装饰器。
更多
如果你还不想使用装饰你将不得不写自己的类型定义,然后覆盖从终极版的那些,这两者都不是简单的挑战。欢迎您解决它们的乐趣,但需要更多的时间可以在有帮助的回答。
请问你是如此友善,并分享一个如何使用@connect与mapstatetoprops,dispatchtoprops和一个commponent的类型安全定义的例子。我很努力。 – Karl2011