TypeScript Static Members
Learn how static properties and methods belong to a class itself rather than to individual objects.
1. What are Static Members?
In TypeScript, the static keyword declares a property
or method that belongs to the class itself, not to individual instances.
A static member is normally accessed using the class name. You do not need to create an object to access it.
Basic Syntax
class Calculator {
static companyName: string = "CIIT";
static add(a: number, b: number): number {
return a + b;
}
}
console.log(Calculator.companyName);
console.log(Calculator.add(10, 20));
Expected Output
CIIT
30
Here, companyName and add() are static members.
Both are accessed directly through the Calculator class.
2. Static Members Workflow Diagram
Understand how static members are declared and accessed without creating an object.
Define Class
Declare a property or method using static.
Class Name
Use the class name to refer to the static member.
Access Member
Read the static property or call the static method.
Result
Get the value or result without creating an instance.
Calculator.add(10, 20), not
new Calculator().add(10, 20), to call a static method.
3. Static Properties
A static property belongs to the class itself. It can store class-level information that is shared rather than being separately declared on each object instance.
Example
class Student {
static collegeName: string = "ABC College";
name: string;
constructor(name: string) {
this.name = name;
}
displayInfo(): void {
console.log("Student: " + this.name);
console.log("College: " + Student.collegeName);
}
}
const student1 = new Student("Rahul");
const student2 = new Student("Priya");
student1.displayInfo();
student2.displayInfo();
Expected Output
Student: Rahul
College: ABC College
Student: Priya
College: ABC College
Both objects use the same class-level collegeName.
Their individual name values are separate.
4. Static Methods
A static method belongs to the class and is called using the class name. It is useful for operations that do not require a particular object's data.
Example: Calculator
class Calculator {
static add(a: number, b: number): number {
return a + b;
}
static multiply(a: number, b: number): number {
return a * b;
}
}
console.log(Calculator.add(10, 5));
console.log(Calculator.multiply(10, 5));
Expected Output
15
50
How It Works
Calculator Class
Defines static arithmetic methods.
add()
Accepts two numbers and returns their sum.
multiply()
Accepts two numbers and returns their product.
5. Static Members vs Instance Members
Instance members belong to individual objects, whereas static members belong to the class.
Example
class Employee {
static companyName: string = "CIIT";
name: string;
constructor(name: string) {
this.name = name;
}
displayInfo(): void {
console.log(this.name + " works at " + Employee.companyName);
}
}
const emp1 = new Employee("Amit");
const emp2 = new Employee("Priya");
emp1.displayInfo();
emp2.displayInfo();
console.log(Employee.companyName);
Expected Output
Amit works at CIIT
Priya works at CIIT
CIIT
| Feature | Static Member | Instance Member |
|---|---|---|
| Belongs to | The class | An object instance |
| Access syntax | Employee.companyName |
emp1.name |
| Requires an object | No | Yes, for instance-based access |
| Typical use | Shared values and utility functions | Object-specific data and behavior |
6. Static Readonly Properties
The combination of static and readonly
is useful for class-level values that should not be reassigned
after initialization through normal TypeScript code.
Example
class AppConfig {
static readonly APP_NAME: string = "CIIT Learning";
static readonly VERSION: string = "1.0";
}
console.log(AppConfig.APP_NAME);
console.log(AppConfig.VERSION);
Expected Output
CIIT Learning
1.0
Both values are available through the class name without creating an object. TypeScript prevents normal reassignment of readonly properties after initialization.
readonly is a compile-time restriction.
It does not automatically provide runtime immutability in JavaScript.
7. Practical Example: Object Counter
A static property can keep a count shared by all instances of a class. This example increments the shared counter whenever an Employee object is constructed.
Example
class Employee {
static count: number = 0;
constructor(public name: string) {
Employee.count++;
}
}
const emp1 = new Employee("Amit");
const emp2 = new Employee("Priya");
const emp3 = new Employee("Rahul");
console.log("Total Employees: " + Employee.count);
Expected Output
Total Employees: 3
Counter Workflow Diagram
Create emp1
Counter becomes 1.
Create emp2
Counter becomes 2.
Create emp3
Counter becomes 3.
Shared Count
Read Employee.count.
8. Important Rules of Static Members
| Rule | Explanation |
|---|---|
| Class-level access | Access static properties and methods through the class name. |
| No instance required | Static members can be used without creating an object. |
| No direct instance access | Static members are not accessed as ordinary instance members. |
| No direct instance state in static methods | A static method cannot use instance properties through this as though it were an object instance. |
| Static readonly | Useful for class-level values that should not be reassigned through normal TypeScript code. |
Summary
Static properties and methods belong to a class rather than individual objects.
Access them with the class name. Static members are useful for shared counters,
configuration values and utility operations. The combination of
static readonly is useful for class-level values that should not
be reassigned through normal TypeScript code.
You have completed the TypeScript OOP module. Next, start Interfaces and Types to learn how to define object contracts and reusable type definitions.