繁体   English   中英

如何定义正确的地图 <X,Y> 输入打字稿,其中Y将是Parent的任何子类?

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

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