Categories
TypeScript Answers

How to create a TypeScript static class?

Spread the love

Sometimes, we want to create a TypeScript static class.

In this article, we’ll look at how to create a TypeScript static class.

How to create a TypeScript static class?

To create a TypeScript static class, we can create an abstract class.

For instance, we write

export abstract class MyClass {
  public static myProp = "Hello";

  public static doSomething(): string {
    return "World";
  }
}

const x = MyClass.doSomething();

to create the MyClass abstract class with the abstract keyword.

In it, we add the doSomething method.

And we call it with MyClass.doSomething.

Conclusion

To create a TypeScript static class, we can create an abstract class.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *