简体   繁体   English

属性“propname”是“Class”类型的静态成员

[英]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.

相关问题 属性“runStaticMethod”是“DemoClass”类型的静态成员 - Property 'runStaticMethod' is a static member of type 'DemoClass' 根据子类中的 static 成员类型确定 class 实例成员类型 - Determine class instance member type based on static member type in subclass 在Typescript中提取静态成员类对象的类型 - Extract type of static member class object in Typescript 如何为静态类成员使用查找类型? - How to use a lookup type for a static class member? 如何在 static 成员中使用 class 类型参数? - How to use class type parameter in static member? 如何让一个class的成员类型static指向子类的类型 - How to make the static member type of a class point to the type of the subclass 转换类类型并调用静态属性 - Casting a class type and calling a static property “此”类型仅在类或接口的非静态成员中可用 - A 'this' type is available only in a non-static member of a class or interface 如何解决“ [对象]上不存在属性[propName]” - How to resolve “property [propName] does not exist on [object]” 键入接受 object 及其属性名称的通用 function 及其属性名称,期望 obj[propName] 为特定类型,例如 sumBy、mapBy - Typing a generic function that accepts an object and its property name, expecting obj[propName] to be of certain type, e. g. sumBy, mapBy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM