abstract class AbstractView {
ctx : AbstractView;
constructor()
{
this.ctx = this.constructor as typeof AbstractView;
}
static getStatus() : string
{
return 'hi';
}
}
class testView extends AbstractView {
static getStatus() : string
{
return 'hi2';
}
}
I am trying to access an overloaded static method from the parent class, however I receive an error message that I don't quite understand.
"Property 'ctx' is missing in type 'typeof AbstractView' but required in type 'AbstractView'"
CodePudding user response:
The error message is telling you the problem:
"Property 'ctx' is missing in type 'typeof AbstractView' but required in type 'AbstractView'"
Make them match!
The following compiles, though storing the constructor looks pretty weird.
abstract class AbstractView {
ctx : typeof AbstractView;
constructor() {
this.ctx = this.constructor as typeof AbstractView;
}
}
CodePudding user response:
Make a typed getter so it has proper type and you don't have to store it
abstract class AbstractView {
get ctx(): typeof AbstractView {
return this.constructor as typeof AbstractView;
}
static getStatus() : string
{
return 'hi';
}
}
class TestView extends AbstractView {
static getStatus() : string
{
return 'hi2';
}
}
console.log(new TestView().ctx.getStatus())
// > "hi2"