Categories
TypeScript Answers

How to add public static const variables in TypeScript?

Spread the love

Sometimes, we want to add public static const variables in TypeScript

In this article, we’ll look at how to add public static const variables in TypeScript.

How to add public static const variables in TypeScript?

To add public static const variables in TypeScript, we can add static getters.

For instance, we write

export class C {
  public static get FOO(): string {
    return "foo";
  }
}

to create the static getter FOO which returns 'foo'.

We make it static with static so we don’t need to instantiate the class to access it.

So we can use it by writing

C.FOO

Conclusion

To add public static const variables in TypeScript, we can add static getters.

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 *