How to Implement Class Constants in TypeScript

To implement class constants in TypeScript, use the “static readonly” modifier. This creates a property that belongs to the class itself (not to its instances) and cannot be modified once it’s initialized.


class MyClass {
  static readonly CONSTANT_VALUE: string = "Yello, Homer!";



Yello, Homer!

In this code example, we defined a class constant called CONSTANT_VALUE with the static readonly modifier.

The constant belongs to the MyClass class and can be accessed using the class name followed by a dot (MyClass.CONSTANT_VALUE). Note that you don’t need to create an instance of the class to access the constant.

Using static readonly properties is a common way to implement class constants in TypeScript, ensuring that the value is associated with the class and cannot be changed after initialization.

Leave a Comment