[英]How to define correct Map<X,Y> type in typescript where Y will be any child class of Parent?
我有一些Parent
类和大量扩展父类的Child
类。 我需要定义Map<sting,Y>
,其中Y
可以是任何Child
类。 如果我尝试将其定义为Map<string, Parent>
则会发生翻译错误
错误TS2345:类型'typeof Child'的参数不能分配给'父母'类型的参数。 属性“ typeof Child”中缺少属性“ someParentMethod”。
因为Child
自动从Parent
继承了someParentMethod
,所以我不在Child
类中进行声明。 声明所需的Map
参数类型以使其接受任何Child
类实例的正确方法是什么?
看起来您只缺少typeof
。
class a { public x; someParentMethod: () => void }
class b extends a { }
let m: Map<string, typeof a> = new Map([['a', b]]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.