[英]How to define correct Map<X,Y> type in typescript where Y will be any child class of Parent?
I have some Parent
class and amount of Child
classes that extends parent. 我有一些
Parent
类和大量扩展父类的Child
类。 I need to define Map<sting,Y>
where Y
can be any of Child
classes. 我需要定义
Map<sting,Y>
,其中Y
可以是任何Child
类。 If I try to define it as Map<string, Parent>
I got transpilation error 如果我尝试将其定义为
Map<string, Parent>
则会发生翻译错误
error TS2345: Argument of type 'typeof Child' is not assignable to parameter of type 'Parent'.
错误TS2345:类型'typeof Child'的参数不能分配给'父母'类型的参数。 Property 'someParentMethod' is missing in type 'typeof Child'.
属性“ typeof Child”中缺少属性“ someParentMethod”。
As Child
automatically inherits someParentMethod
from Parent
I don't re declare in Child
class. 因为
Child
自动从Parent
继承了someParentMethod
,所以我不在Child
类中进行声明。 What is the correct way of declare needed Map
argument type, so that it accepted any Child
class instances? 声明所需的
Map
参数类型以使其接受任何Child
类实例的正确方法是什么?
Looks like you are only missing a typeof
. 看起来您只缺少
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.