How to add public static const variables in TypeScript?

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.