How to implement class constants in TypeScript ?
Last Updated :
07 Apr, 2022
In this article, we will try to understand how to create several class constants (properties with constant values) in TypeScript with the help of certain code examples for better concept understanding as well as clarification.
Let us first understand quickly how we may create a class in TypeScript with the following syntax-
Syntax: Use the following syntax provided by TypeScript for creating a class-
class class_name {
// Some codes....
}
The following examples would help us to understand the above syntax for the creation of the class.
Example 1: In this example, we will simply create a class and thereafter will use data inside it by creating its own instance (or object) while calling any particular data.
JavaScript
class DisplayName {
name: string = "GeeksforGeeks"
}
let instance = new DisplayName();
console.log(instance.name);
Output:
GeeksforGeeks
Now, we have understood how to create a class in TypeScript. Let us see how to create a class having constant fields or properties with values that are not alterable or changeable.
Example 2: In this example, we will create a class with the same syntax which has been illustrated above. Here we will be using a keyword called readonly which will eventually help us to make that particular field value non-alterable. If we try to change that value then as a result an error message on the user's browser's console.
JavaScript
class DisplayResult{
readonly name : string = "GeeksforGeeks";
displayResult() : void{
console.log(this.name);
// this.name = "Aman"
// Display an error message since
// it is readonly class property...
}
}
let instance = new DisplayResult();
instance.displayResult();
Output:
GeeksforGeeks
Example 3: In this example, we would be creating a class again with the same syntax provided above. Along with readonly keyword in this, we would be using another keyword called static which would eventually help us to directly instantiate that constant property without creating any specific instance for the same.
In other words, this class property would remain constant and not assignable to the class constructor if we use the above-illustrated thing. Also for simplicity, we would be declaring here the static method (which doesn't require any instance instantiation while calling) which will be responsible for displaying our result over the browser's console.
JavaScript
class DisplayResult{
static readonly username : string = "GeeksforGeeks";
static displayResult() : void {
// DisplayResult.username = "Aman"
// Display an error message since
// it is readonly class property...
console.log(DisplayResult.username);
}
}
DisplayResult.displayResult();
Output:
GeeksforGeeks
Similar Reads
How to Implement a TypeScript Decorator ? In TypeScript, decorators are a feature that allows you to attach metadata to classes, methods, properties, or parameters concisely and expressively. TypeScript decorators are applied using the @decorator syntax. In this article, we are going to learn how to implement a typescript decorator. How to
2 min read
How to Create Nested Classes in TypeScript ? In TypeScript, you can create nested classes using different methods. We will discuss about three different approaches to creating nested classes in TypeScript. These are the approaches: Table of Content By defining nested classes inside a classBy using the namespacesBy using the modulesBy defining
3 min read
How to Convert a String to enum in TypeScript? In TypeScript, an enum is a type of class that is mainly used to store the constant variables with numerical and string-type values. In this article, we will learn, how we can convert a string into an enum using TypeScript.These are the two approaches that can be used to solve it:Table of ContentUsi
5 min read
How to use class syntax in Typescript ? Classes: The class keyword was introduced in ES2015. TypeScript fully supports 'class' keyword. classes are a template for creating objects. A Class is a user defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions use
2 min read
How to Access TypeScript Private Members ? In this TypeScript article, we will learn how to access the TypeScript private members using different approaches. We can interact with the encapsulated components within the class and access them in the application. There are three different approaches through which we can access TypeScript private
3 min read
How to implement Type narrowing in TypeScript? Type narrowing in TypeScript refers to refining the type of a variable within a conditional block based on runtime checks. This is achieved through techniques like typeof guards, instance checks, or property existence checks, enabling more precise typing and avoiding type errors in subsequent code e
2 min read