Imagine
abstract class A {
protected abstract f(): void;
}
class B extends A {
public f() { return 'B'; }
}
class C extends A {
public f() { return 'C'; }
}
If you want an array that contains B
and C
, you can achieve it by:
let arr: Array<B | C> = [/* lots of objects */];
arr.forEach(item => item.f());
But if you want to make Array<B | C>
more flexible and general, so it would be Array</*descendant of A*/>
.
Any suggestions on how to approach this?