简体   繁体   English

如何在 TypeScript 中使用抽象 class 作为 React 组件的类型?

[英]How to use an abstract class as the type of a React component in TypeScript?

How can the abstract class 'Base' be used as a type definition here?抽象 class '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 />);
  }
}

Change the props from改变道具从

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

to

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

See the TS docs and this question for more information.有关更多信息,请参阅TS 文档此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Typescript 中扩展 React 抽象组件 - Extend React Abstract Component in Typescript 如何使用 jest 在 typescript 中测试抽象 class? - How to use jest for testing abstract class in typescript? 如何在组件(React + TypeScript)中输入链接属性? - How to type link prop in component (React + TypeScript)? 基于 class 的 React 组件使用什么类型? - What type to use for a class based React component? 如何在打字稿中为aa子类(React.Component)编写类型定义? - How do I write a type definition for a a sub class (React.Component) in typescript? React Class 组件属性中的 TypeScript 错误在类型“Readonly&lt;{}&gt;”上不存在,不确定如何为 state 设置类型 - TypeScript errors in React Class Component property does not exist on type 'Readonly<{}>', not sure how to set types for state Typescript:抽象泛型 class 的子类类型 - Typescript: type of subclasses of an abstract generic class 如何将带有通用道具的React类组件转换为Typescript中的功能组件? - How to transform a React Class Component with generic props to Function Component in Typescript? 如何在 TypeScript 中使用 no-typescript React 组件? - How can I use no-typescript React component in TypeScript? 在 React class 组件中使用 typescript generic 将 props 连接到 state - Use typescript generic to connect props to state in React class component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM