I'm encountering an issue with these classes. I want to utilize the doSomething()
method that is exclusive to class B
without having to type cast it each time. However, when I specify property a
to be of type B
, it gives me an error saying it's not assigned in the constructor, even though the parent constructor handles the assignment.
class A {
}
class B extends A {
doSomething() { }
}
class One {
constructor(protected a: A){ }
}
class Two extends One {
protected a: B // Property 'a' has no initializer and is not definitely assigned in the constructor.
constructor(){
super(new B());
// If I add "this.a = new B();" here, the error disappears but it seems redundant.
}
doStuff() {
this.a.doSomething()
}
}
What am I doing incorrectly?