There is an abstract typescript class like this:
abstract class Abstract {
constructor (public parent?: Abstract) {
}
}
Then, two subclasses are defined as follows:
class Sub1 extends Abstract {
}
class Sub2 extends Abstract {
}
The issue arises when the parent property in both subclasses can be of type Abstract, which allows instances like:
let sub1 = new Sub1 ();
let sub2 = new Sub2 (sub1);
The requirement is to enforce that Sub1.parent
must have a type of Sub1
and Sub2.parent
must have a type of Sub2
. How can this be achieved in the definition of Abstract?