组件拆分中的构造函数连接Typescript中的react-redux

时间:2018-03-02 11:39:02

标签: reactjs typescript redux react-redux containers

我有一个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

1 个答案:

答案 0 :(得分:0)

因此,如果我写constructor(props?: ITestClassProps)它似乎有效。我在@types中看到构造函数的签名有props?,可能它们之间存在冲突