Data Modifiers in TypeScript

In TypeScript, data modifiers are used to define the access level of class properties and methods. These modifiers determine whether a property or method can be accessed from outside of the class or not. There are three types of data modifiers in TypeScript: public, private, and protected.

  1. Public modifier: A public modifier is the default modifier for class members in TypeScript. A public property or method can be accessed from anywhere, both inside and outside of the class. You can also explicitly use the keyword “public” to indicate that a class member is public.

Here’s an example:

class Person {
    name: string;
    age: number;
    
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    
    public getDetails(): void {
        console.log(`Name: ${this.name}, Age: ${this.age}`);
    }
}

let person1 = new Person("John", 25);
person1.getDetails(); // Output: Name: John, Age: 25

In the example above, the “name” and “age” properties of the “Person” class are public by default. We also declared a public method called “getDetails” which can be accessed from anywhere.

  1. Private modifier: A private modifier restricts access to a property or method within the class that defines it. A private property or method cannot be accessed from outside of the class. To declare a private member, we use the keyword “private”.

Here’s an example:

class Person {
    private name: string;
    private age: number;
    
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    
    public getDetails(): void {
        console.log(`Name: ${this.name}, Age: ${this.age}`);
    }
}

let person1 = new Person("John", 25);
person1.getDetails(); // Output: Name: John, Age: 25
console.log(person1.name); // This will result in an error: 'name' is private and can only be accessed within class 'Person'.

In the example above, we declared the “name” and “age” properties as private. We also declared a public method called “getDetails” that can access these private properties. However, when we try to access the “name” property outside of the class, we get an error because it is private.

  1. Protected modifier: A protected modifier restricts access to a property or method within the class that defines it and any subclasses. A protected property or method cannot be accessed from outside of the class or its subclasses. To declare a protected member, we use the keyword “protected”.

Here’s an example:

class Person {
    protected name: string;
    protected age: number;
    
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

class Employee extends Person {
    private salary: number;
    
    constructor(name: string, age: number, salary: number) {
        super(name, age);
        this.salary = salary;
    }
    
    public getDetails(): void {
        console.log(`Name: ${this.name}, Age: ${this.age}, Salary: ${this.salary}`);
    }
}

let employee1 = new Employee("John", 25, 50000);
employee1.getDetails(); // Output: Name: John, Age: 25, Salary: 50000
console.log(employee1.name); // This will result in an error: 'name' is protected and can only be accessed within class 'Person' and its subclasses.

In the example above, we declared the “name” and “age” properties of the “Person” class as protected. We also created a subclass called “Employee” that extends the “Person” class. The “Employee” class has a private property called “salary” and a public method called “getDetails” that can access the protected properties of the “Person” class. When we create an instance of the “Employee” class and call the “getDetails” method, we can see that it can access the protected “name” and “age” properties.

However, when we try to access the “name” property outside of the “Person” or “Employee” classes, we get an error because it is protected.

In conclusion, data modifiers in TypeScript allow you to control the accessibility of class properties and methods. By default, properties and methods are public, but you can also declare them as private or protected to restrict access. Private members can only be accessed within the class that defines them, while protected members can also be accessed within subclasses.