What is Public Static Const in TypeScript ?
Last Updated :
28 Apr, 2025
In TypeScript, there is no direct equivalent to a public static const
declaration as you might find in some other languages. However, you can achieve similar behavior using a combination of the public
, static
, and readonly
keywords.
Syntax:
class ClassName {
public static readonly CONSTANT_NAME: Type = InitialValue;
// Other members of the class...
}
Let's break down what each of these modifiers does:
public
: This keyword declares that the constant is accessible from outside the class. It means that the constant can be accessed by instances of the class as well as from outside the class.static
: The static
the keyword indicates that the constant is associated with the class itself, not with instances of the class. It means you can access the constant using the class name without creating an instance. For example, if you have a class MyClass
with a static constantMY_CONSTANT
, you can access it MyClass.MY_CONSTANT
without creating an instance of MyClass
.readonly
: This keyword specifies that the constant cannot be reassigned after it is initialized. Once a value is assigned to a readonly
property or constant, it cannot be changed. This ensures that the constant maintains its initial value throughout the program.
Example 1: In this example, the class MathConstants
declares a public, static, and read-only constant named PI
with the value 3.14159
. The class also has a method calculateCircumference
that uses the PI
constant to calculate the circumference of a circle. The constant can be accessed using MathConstants.PI
.
JavaScript
class MathConstants {
public static readonly PI: number = 3.14159;
calculateCircumference(radius: number): number {
return 2 * MathConstants.PI * radius;
}
}
// Example usage:
const mathInstance = new MathConstants();
const circumference = mathInstance
.calculateCircumference(5);
console.log(`Circumference: ${circumference}`);
Output:
Circumference: 31.4159
Example 2: In this simple example, the SimpleExample
class declares a public, static, and read-only constant named GREETING
with the value "Hello, TypeScript!"
. The class also has a method printGreeting
that prints the greeting to the console. The constant is accessed using SimpleExample.GREETING
.
JavaScript
class SimpleExample {
public static readonly GREETING:
string = "Hello, TypeScript!";
printGreeting(): void {
console.log(SimpleExample.GREETING);
}
}
// Example usage:
const instance = new SimpleExample();
instance.printGreeting();
Output:
Hello, TypeScript!
Similar Reads
What is namespace in Typescript ? In TypeScript, a namespace is a way to organize code logically and prevent naming conflicts between identifiers. It allows developers to group related functionalities, such as interfaces, classes, functions, and variables, within a dedicated scope.Namespaces are particularly useful for structuring l
3 min read
What is Type Erasure in TypeScript? TypeScript is a very mighty superset of JavaScript, which adds static typing to the language and allows developers to catch errors early on as well as write more maintainable code. This being said a TypeScript type system erases type information at compile time (or during the compilation), a phenome
4 min read
What is readonly in Typescript ? This article will cover the readonly keyword in TypeScript, which is a powerful superset of JavaScript that adds extra features and syntax to the language. The readonly keyword is one of these features, used to set a property or variable as read-only, meaning that the value cannot be changed once it
3 min read
Whatâs new in TypeScript 5.4 ? TypeScript 5.4 brings forth several new capabilities and refinements, aiming to enhance developer productivity and code quality. Some of the new capabilities that are added or Introduced in TypeScript 5.4 are as follows: Table of Content Variadic Tuple TypesClass Property Inference from Constructors
2 min read
What are the Modules in Typescript ? Modules in TypeScript allow you to organize code into reusable, manageable, and logical units by encapsulating functionalities into separate files.They help avoid global namespace pollution by providing scoped declarations.Modules can be imported and exported, enabling code reuse and better maintain
4 min read