Access Modifiers in TypeScript

The TypeScript access modifiers are used to regulate how different class members will be seen such as properties or methods, they have great significance in support of the Object-Oriented Programming principle of Encapsulation and information hiding, TypeScript has three types of access modifiers that are public, private and protected.

Types of Access Modifiers

1) Public Access Modifier

The modifier is public and facilitates access to class members within and outside of the class by anyone in the program. It is a default access modifier in TypeScript class members, so you do not have to mention it explicitly unless accessibility is your concern.

Example: In the Animal class, the name is declared public, making it available outside of the class, using dot notation, we may get the name of an Animal object (dog).

JavaScript
class Animal {
  public name: string;

  constructor(name: string) {
    this.name = name;
  }

  public makeSound(): void {
    console.log(`${this.name} makes a sound.`);
  }
}
const dog = new Animal('Dog');
console.log(dog.name); 
dog.makeSound();

Output:

Dog
Dog makes a sound.

2) Private Access Modifier

The private modifier stops class member from being accessed by others except for the class it is declared in, it’s good for keeping secret its implementation details and protecting an object’s inner state.

Example: Person declares quantity as private, preventing direct access from the outside, the public getSSN method can change the quantity while verifying its value (accessing quantity).

JavaScript
class Person {
  private ssn: string;

  constructor(ssn: string) {
    this.ssn = ssn;
  }

  public getSSN(): string {
    return this.ssn;
  }
}

const person = new Person('123-45-6789');
console.log(person.getSSN()); 
// Output: 123-45-6789


 // Error: Property 'ssn' is private and
 // only accessible within class 'Person'.
// console.log(person.ssn);

Output:

123-45-6789

3) Protected Access Modifier

The protected keyword is used to declare a class member so that it can be accessed by the class containing it and any of its subclasses, it comes handy when you want members of a class accessed in descendant classes but not outside.

Example: Age is protected in User, enabling access within User and its subclass Employee. Employee’s getRetirementAge method calculates retirement age using age (protected).

JavaScript
class User {
// Accessible in User and subclasses
    protected age: number; 

    constructor(age: number) {
        this.age = age;
    }
}

class Employee extends User {
    public getRetirementAge(): number {
    // Access protected member from subclass
        return this.age + 65; 
    }
}
// newUser.age is NOT accessible (protected)
const newUser = new User(25); 

// newUser.age is NOT accessible (protected)ee(30);
const newEmployee = new Employ

// Output: 95
console.log(newEmployee.getRetirementAge()); 

Output:

95

Conclusion

Visibility and accessibility of class members can be controlled by typescript access modifiers, by appropriately using public, private, protected access modifiers, you will have better encapsulation, data hiding and inheritance.