TypeScript Access Modifiers
Learn how public, private and protected modifiers control access to class properties and methods in TypeScript.
1. What are Access Modifiers?
Access modifiers specify the accessibility of properties and methods within a TypeScript class.
TypeScript provides three main access modifiers: public, private and protected. If you do not specify a modifier, class members are public by default.
Types of Access Modifiers
Public
Accessible from anywhere the object or class member is available.
Open AccessPrivate
Accessible only within the class that declares the member.
Class OnlyProtected
Accessible within the declaring class and its subclasses.
Class + Subclass2. Access Modifiers Workflow Diagram
The following diagram shows where each type of class member can be accessed.
Class Member
A property or method is declared inside a class.
Access Modifier
TypeScript checks whether the caller is allowed to access the member.
Access Result
Access is permitted or a type-checking error is reported.
3. Public Access Modifier
The public modifier allows a property or method to be accessed
from outside the class as well as from within the class.
It is also the default visibility for class members in TypeScript.
Example
class Student {
public name: string = "Rahul";
public displayName(): void {
console.log(this.name);
}
}
const student1 = new Student();
console.log(student1.name);
student1.displayName();
Expected Output
Rahul
Rahul
Explanation
The name property and displayName() method are public.
Therefore, the object can access both of them directly.
4. Private Access Modifier
The private modifier restricts access to a member to the class
that declares it. It cannot normally be accessed directly through an object
or from a subclass during TypeScript type checking.
Example
class Employee {
private salary: number = 30000;
public displaySalary(): void {
console.log("Salary: " + this.salary);
}
}
const emp1 = new Employee();
emp1.displaySalary();
// This would cause a TypeScript error:
// console.log(emp1.salary);
Expected Output
Salary: 30000
Private Member Workflow
Outside Object
Attempts to read the private salary directly.
Private Check
TypeScript rejects direct access from outside the class.
Public Method
The object can call displaySalary() to use the permitted operation.
private modifier is enforced by its type checker.
JavaScript's runtime-private fields use the #field syntax instead.
5. Protected Access Modifier
The protected modifier allows access within the class that
declares the member and within its subclasses. It does not allow ordinary
direct access through an object outside the class hierarchy.
Example
class Person {
protected name: string = "Rahul";
}
class Student extends Person {
public displayName(): void {
console.log(this.name);
}
}
const student1 = new Student();
student1.displayName();
// This would cause a TypeScript error:
// console.log(student1.name);
Expected Output
Rahul
Protected Access Diagram
Parent Class
Declares the protected name property.
Child Class
Inherits the property and accesses it through this.name.
Outside Object
Cannot directly access the protected property.
6. Public vs Private vs Protected
| Feature | Public | Private | Protected |
|---|---|---|---|
| Inside declaring class | Yes | Yes | Yes |
| Directly through an outside object | Yes | No | No |
| Access from a subclass | Yes | No | Yes |
| Typical use | Exposed functionality | Class-specific internal details | Members shared with subclasses |
7. Practical Example: Employee Management
This example combines all three access modifiers in a simple employee class.
class Employee {
public name: string;
private salary: number;
protected department: string;
constructor(
name: string,
salary: number,
department: string
) {
this.name = name;
this.salary = salary;
this.department = department;
}
public showDetails(): void {
console.log("Name: " + this.name);
console.log("Salary: " + this.salary);
console.log("Department: " + this.department);
}
}
class Manager extends Employee {
public showDepartment(): void {
console.log("Department: " + this.department);
}
}
const manager1 = new Manager("Priya", 50000, "IT");
console.log(manager1.name);
manager1.showDetails();
manager1.showDepartment();
Expected Output
Priya
Name: Priya
Salary: 50000
Department: IT
Department: IT
Understanding the Example
| Member | Modifier | Access Rule |
|---|---|---|
name |
public | Accessible directly through manager1. |
salary |
private | Accessible within Employee, including its own methods. |
department |
protected | Accessible within Employee and Manager. |
Summary
TypeScript provides public, private and protected access modifiers. Public members can be accessed outside the class, private members are restricted to their declaring class, and protected members are available to the declaring class and its subclasses.
Next, learn abstract classes and how they define a common structure for derived classes.