0
votes

I have some Parent class and amount of Child classes that extends parent. I need to define Map<sting,Y> where Y can be any of Child classes. If I try to define it as Map<string, Parent> I got transpilation error

error TS2345: Argument of type 'typeof Child' is not assignable to parameter of type 'Parent'. Property 'someParentMethod' is missing in type 'typeof Child'.

As Child automatically inherits someParentMethod from Parent I don't re declare in Child class. What is the correct way of declare needed Map argument type, so that it accepted any Child class instances?

1
Is the Map supposed to hold instances of Child or constructors of Child? Please post the code that gives the error. It looks like you are passing in a constructor where it expects an instance. - jcalz
@jcalz Thanks. The problem is exactly that I'm passing constructor, instead of instance. I actually need to pass the constructors. Would you be so kind, to tell ss there a way to define type so that it accepted constuctor of any Child. Example simplified code that raises mentioned error is below. Thanks in advance. ` class a { public x; } class b extends a { } let m:Map<string, a> = new Map([['a',b]]); ` - user3687562

1 Answers

1
votes

Looks like you are only missing a typeof.

class a { public x; someParentMethod: () => void }
class b extends a { }

let m: Map<string, typeof a> = new Map([['a', b]]);