TypeScript static property and static method example

This post shows how to create a static property and static methods in TypeScript with an example.

TypeScript allows creating static members of a class, those that are visible on the class itself rather than on the instances. Static members are referenced by the class name.

TypeScript static property and static method example

In below example, we have two static class members, one is static property and another static method:
class Employee{
    static fullName: string;

    static getFullName(){
        return Employee.fullName;
    }
}

// create Employee class object
Employee.fullName = 'Ramesh Fadatare';
console.log(Employee.getFullName());
Output:
Ramesh Fadatare

Comments