【问题标题】:How to use an abstract class as the type of a React component in TypeScript?如何在 TypeScript 中使用抽象类作为 React 组件的类型?
【发布时间】:2022-01-21 15:21:00
【问题描述】:

抽象类'Base'在这里如何用作类型定义?

abstract class Base extends React.Component { /* */ }

class A extends React.Component<{
  ComponentToUse: Base
}> {
  render() {
    const { ComponentToUse } = this.props;
    // The following line gives this compile error:
    // JSX element type 'Base' does not have any construct or call signatures. ts(2604)
    return (<ComponentToUse />);
  }
}

【问题讨论】:

    标签: javascript reactjs typescript abstract


    【解决方案1】:

    改变道具

    class A extends React.Component<{
      ComponentToUse: Base
    }> {
    

    class A extends React.Component<{
      ComponentToUse: typeof Base
    }> {
    

    请参阅TS docsthis question 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 2019-11-18
      • 2020-05-23
      • 1970-01-01
      相关资源
      最近更新 更多