繁体   English   中英

typescript:获取接口中基本类的类型

[英]typescript: get the type of basic class in interface

我有一个基础课,就像

class Animal {

}

我有两个孩子班

class Dog extends Animal {}
class Bird extends Animal {}

现在,我想定义一个包含Animal类的接口,例如

interface Zoo{
  name: string;
  animal: Animal;
} 

并使用它

const myZoo: Zoo = {
  name: 'zooName',
  animal: Dog  // problem is here
}

顺其自然,我失败了。 所以我该怎么做? 谢谢。

您需要使用typeof运算符定义接口

interface Zoo {
   name: string;
   animal: typeof Animal;
} 

这是一个DEMO

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM