TypeScript Access Modifiers

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


Access modifiers in TypeScript control the visibility and accessibility of class members (properties and methods), helping to enforce encapsulation. For example,

class Person {
  private age: number;
  
  public greet() {
    // code
  }
}

Here, the variable age of the Person class is hidden using the private keyword, while the greet() function is made accessible using the public keyword.


Types of Access Modifiers

In TypeScript, there are 3 access modifiers:

  • Public
  • Private
  • Protected

Public Access Modifier

The public modifier allows class members to be accessible from anywhere in the program–inside the class, outside it, or even from another file. For example,

class Person {
  public name: string;

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

  public greet(): void {
    console.log(`Hello, my name is ${this.name}`);
  }
}

const person = new Person("Alex");
console.log(person.name);  
person.greet();   

Output

Alex
Hello, my name is Alex

Here,

  • name is accessed directly from outside the class and printed.
  • greet() is also accessed from outside and runs successfully.

Note: public is the default access level in TypeScript. If no modifier is used, the member is considered public by default.


Private Access Modifier

The private modifier in TypeScript restricts access to a class member so that it can only be used within the class where it is declared. For example,

class Person {
  private age: number;

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

  printAge(): void {
    console.log(`Age is ${this.age}`); 
  }
}

const person = new Person(25);
person.printAge();

Output

Age is 25

Here, age is printed using the printAge() method because it is accessed from within the same class, which is allowed.

However, if we try to access age directly from outside the class as:

console.log(person.age)

It will result in a compile-time error, because age is marked as private and cannot be accessed from outside the class.


Protected Access Modifier

The protected modifier in TypeScript allows a class member to be accessed within the class itself and in its subclasses, but not from outside the class hierarchy. For example,

class Person {
  protected gender: string;

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

class Student extends Person {
  showGender(): void {
    console.log(`Gender is ${this.gender}`);
  }
}

const student = new Student("Female");
student.showGender();

Output

Gender is Female

Here, the gender property is marked as protected, so it can't be accessed directly from outside the class.

But it works inside the Student subclass because protected allows access in child classes.


Summary

  • public members can be accessed from anywhere in the program.
  • private members are only accessible within the class where they are defined.
  • protected members are like private, but they can be accessed in derived (child) classes.
Specifiers Same Class Derived Class Outside Class
public Yes Yes Yes
private Yes No No
protected Yes Yes No

Read More:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges