Types in Angular/Typescript interfaces

120 views Asked by At

In my app I have BaseObjectInterface and I extend it in many interfaces like AObjInterface, BObjInterface, ect. Then I have parent interface that has array of BaseObjectInterface in it. I want it to accept all classes that extend base one. For example AObjInterface, BObjInterface, etc. Sadly it doesn't work that way. Can I do it any other way than explicitly listing all types?

What I have: list: Array<BaseObjectInterface>

What I don't want: list: Array<AObjInterface|BObjInterface>

What I want put in that list: [ New BaseObjectInterface(), new AObjInterface(), new BObjInterface()]

1

There are 1 answers

1
Daniel Kucal On

If, as you say, BaseObjectInterface is a parent of AObjInterface and BObjInterface, then the type can be just Array<BaseObjectInterface>. TypeScript Playground example