我无法弄清楚为什么此代码无法构建并出现以下错误:
(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>'.
我正在运行 typescript 2.3.3,@types/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()} />
我想避免装饰器,只要它们是实验性的。
我想避免装饰器,只要它们是实验性的。
错误的接近方式。它们就像 JSX 一样工作。当前的类型定义假设您将使用装饰器。
如果您仍然不想使用装饰器,则必须编写自己的类型定义,然后覆盖 redux 中的类型定义,这两个都不是简单的挑战。欢迎您为了好玩而解决它们,但需要的时间比有用的SO 答案中所能提供的要多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句