我无法弄清楚为什么此代码无法构建并出现以下错误:
(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()} />
我想避免装饰器,只要它们是实验性的。
最佳答案
I want to avoid decorators as long as they are experimental.
处理方法错误。它们的工作方式就像 JSX 的工作方式。当前的类型定义假设您将使用装饰器。
更多
如果您仍然不想使用装饰器,则必须编写自己的类型定义,然后覆盖 redux 中的类型定义,这两者都不是简单的挑战。欢迎您出于乐趣来解决这些问题,但需要的时间比有用SO答案中所能提供的时间要多。
关于reactjs - React-Redux connect() 和错误 TS2345,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44370696/