[英]Property 'propname' is a static member of type 'Class'
This code does not compile with typescript.这段代码不能用打字稿编译。 It says
Property 'say' is a static member of type 'Animal
on line 11 (last).它说
Property 'say' is a static member of type 'Animal
第 11 行(最后一个) Property 'say' is a static member of type 'Animal
。
Could someone please explain why?有人可以解释为什么吗?
class Animal {
static say: string;
}
class Dog extends Animal{
static say = 'bark'
}
class Cat extends Animal{
static say = 'meow'
}
const animals:Animal[] = [Dog, Cat]
animals.filter(e=>e.say ==='meow')
You've narrowed the type on animals
to be instances of those classes, not the classes themselves.您已将
animals
的类型缩小为这些类的实例,而不是类本身。 Try this:尝试这个:
const animals: (typeof Animal)[] = [Dog, Cat];
or just leave off the type and let it be inferred;或者只是去掉类型并让它被推断;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.