I'm trying to construct a base class that would be inherited by some sub classes. Inside the base class, I'd like to add multiple static creator functions that'd be easy to use in the codebase. The problem is, I can't access the type of the sub class from the base class.
For example, this is the base class:
class BaseClass {
static fromText(text: string) {
const element = getByText(text);
return new this(element);
}
static fromRole(role: string) {
const element = getByRole(role);
return new this(element);
}
}
And this is the sub class:
class SubClass extends BaseClass {
doSomething() {
console.log('doing something');
}
}
Now, I want to call doSomething from SubClass. The problem is, TypeScript doesn't know this function exist inside SubClass:
const subclass = SubClass.fromText('hello');
subclass.doSomething(); // TypeScript error here, saying doSomething doesn't exist
Does anyone know how can I achieve this?