Based on the feedback provided, here is a solution to achieve your desired outcome:
interface ConfigConstructor {
CoreInterface: () => any;
new (): Config;
}
interface Config {
readonly NAME: string;
readonly TITLE: string;
}
const Test: ConfigConstructor = class Test implements Config {
readonly NAME: string;
readonly TITLE: string;
static CoreInterface = function (): any { return "something"; }
}
(code in playground)
If you remove one of the members (e.g., NAME
), you will encounter the following error:
Class 'Test' does not correctly implement interface 'Config'.
The property 'NAME' is missing in type 'Test'.
If you remove the static CoreInterface
, you will get this error instead:
Type 'typeof Test' cannot be assigned to type 'ConfigConstructor'.
Property 'CoreInterface' is missing in type 'typeof Test'.
Initial response
Static members and methods do not support inheritance, as pointed out by @JBNizet. Static properties belong to the class itself, not its instances.
As mentioned in the Wikipedia article:
Static methods can be called even without existing instances of the class.
They are resolved at compile time based on the class they are called on,
unlike instance methods that are determined dynamically based on object runtime types.
Consequently, static methods cannot be overridden.
For further insight, refer to this discussion: Why aren't static methods considered good OO practice?
Although you won't receive compilation errors for omitting the implementation of a static method when extending a class, you may encounter runtime errors:
class A {
static fn() {
throw new Error("not implemented!");
}
}
class B extends A {
static fn() {
console.log("B.fn");
}
}
class C extends A { }
B.fn(); // success
C.fn(); // error: not implemented!
(code in playground)