add a public static method to a class in typescript

To add a public static method to a class in TypeScript, you can use the keyword static before the method name. Here's an example:

index.ts
class MyClass {
    static myStaticMethod() {
        console.log('This is a static method.');
    }
}

MyClass.myStaticMethod(); // Output: This is a static method.
166 chars
8 lines

In the example above, we created a class named MyClass with a public static method named myStaticMethod. This method can be called directly on the class itself without creating an instance of the class.

To call the method, we simply use the class name followed by the method name, like this: MyClass.myStaticMethod();.

gistlibby LogSnag