Static factory method in abstract class
this
is typeof Component
, and since Component
is abstract class, new this
is inadmissible. Even if Component
wasn't abstract, Button.build()
return type wouldn't be properly handled in child classes.
It requires some type hinting, via generic method:
abstract class Component {
static build<T = Component>(this: { new(): T }) {
return new this();
}
}
class Button extends Component { }
const button = Button.build<Button>(); // button: Button