【问题标题】:Constructor in component breaks connect react-redux in Typescript组件中断中的构造函数连接 Typescript 中的 react-redux
【发布时间】: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


【解决方案1】:

所以看来如果我写constructor(props?: ITestClassProps) 它可以工作。我在@types 中看到构造函数的签名有props?,它们之间可能存在冲突

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多