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

问题描述 投票:0回答:1

我有一个基本课程就像

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
}

说实话,我失败了。所以我该怎么做?谢谢。

javascript typescript
1个回答
1
投票

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

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

这是一个DEMO

© www.soinside.com 2019 - 2024. All rights reserved.