强制子类在重写方法上调用 super

Ser*_*iro 6 typescript

在扩展类时,有时我们会忘记在已经实现的方法中调用超类实现。

当一个方法被覆盖而不是调用 super 时,有没有办法通过抛出错误或其他东西来防止这种情况发生?


例子

class MainClass implements OnInit
{
    ngOnInit() {
        // base implementation
    }
}

class InnerClass extends MainClass
{
    ngOnInit() {
        super.ngOnInit(); // ---> throw error if didn't call this line
        // additional implementation
    }    
}
Run Code Online (Sandbox Code Playgroud)

sha*_*hal 1

github上有一个关于它的有趣讨论,这个词concrete被提出了,但是没有,目前TS不支持它。您必须为此使用一些解决方法(也在该页面上列出)。