I have this simple code in TypeScript:
abstract class Config {
readonly NAME: string;
readonly TITLE: string;
static CoreInterface: () => any
}
class Test implements Config {
readonly NAME: string;
readonly TITLE: string;
}
Even though the CoreInterface() member is missing in the Test class, TypeScript does not complain. Why is this?
I need every derived class to provide some metadata about itself in the CoreInterface() static function. I know I could just extend the Config class and have each sub-class provide its own implementation of CoreInterface(), but I do not want sub-classes to automatically inherit any of the members of the COnfig class. That is why I use "implements" instead of "extends"
Based on your comment, here's how you can achieve what you're looking for:
(code in playground)
If you comment out one of the members (i.e.:
NAME
) you'll get this error:If you comment out the static
CoreInterface
you'll get this error:Original answer
Static members/methods don't work with inheritence (that's true to OO in general and not specific to typescript) because (as @JBNizet commented) all static properties belong to the class itself and not to the instances.
As written in Wikipedia article:
Also check this thread: Why aren't static methods considered good OO practice?
As for what you want to accomplish, you won't be able to get compilation errors for not implementing the static method when extending the class, but you can get runtime errors:
(code in playground)