【发布时间】:2018-08-10 14:58:35
【问题描述】:
我有以下 Typescript 类的示例。我在“react-redux”(最后一行代码)的连接函数上有一个错误,我不明白为什么。我需要那里的构造函数来处理其他事情,似乎因此出现了错误。有什么想法吗?
interface ITestClassProps extends IStateProps, IDispatchProps {}
interface IStateProps {
isDisabled: boolean;
}
interface IDispatchProps {
onClick: () => void;
}
class TestClass extends PureComponent<ITestClassProps, {}> {
constructor(props: ITestClassProps) {
super(props);
...
}
public render() {
return (
<div />
);
}
}
const mapStateToProps = (state: any): IStateProps => {
return {
isDisabled: state.isLoading
};
};
const mapDispatchToProps = (dispatch: any): IDispatchProps => ({
onClick: actions.onClick
});
default connect<IStateProps, IDispatchProps, {}>(
mapStateToProps,
mapDispatchToProps
)(TestClass);
错误:
[ts]
Argument of type 'typeof TestClass' is not assignable to parameter of type 'Component<IStateProps & IDispatchProps>'.
Type 'typeof TestClass' is not assignable to type 'StatelessComponent<IStateProps & IDispatchProps>'.
Type 'typeof TestClass' provides no match for the signature '(props: IStateProps & IDispatchProps & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.
class TestClass
【问题讨论】:
-
无法使用您的代码重现,如果
props具有不同的类型或ITestClassProps没有实现这两个接口(使用npm 的最新定义测试),我只会收到此错误 -
我有 "react-redux": "4.4.5" 和 "@types/react-redux": "4.4.47"
-
仍然有效,TS 2.7.2 & @types/react-redux@4.4.47
-
这里一样 :( 它仍然会发生
标签: reactjs typescript redux react-redux containers