call a static method on an instance of a class in typescript

To call a static method on an instance of a class in Typescript, you must reference the class name, followed by the static method name, like so:

index.ts
class MyClass {
  static myStaticMethod() {
    console.log('Hello from myStaticMethod!');
  }
}

const myInstance = new MyClass();
MyClass.myStaticMethod(); // "Hello from myStaticMethod!"
myInstance.myStaticMethod(); // Error: "Property 'myStaticMethod' does not exist on type 'MyClass'"
290 chars
10 lines

As shown in the code snippet above, attempting to call a static method on an instance of a class in Typescript would result in a compilation error, as static methods belong to the class itself, rather than its instances. Therefore, it is vital to call static methods directly from the class to avoid errors.

gistlibby LogSnag