What does the private access modifier do?
The private modifier in TypeScript is used to hide a class property or method, making it accessible only inside the class itself.
This is a means of encapsulation that helps protect internal data from outside interference.
1. Syntax
class User {
private password: string;
constructor(password: string) {
this.password = password;
}
private encryptPassword(): string {
return this.password.split("").reverse().join("");
}
public checkPassword(pwd: string): boolean {
return this.encryptPassword() === pwd.split("").reverse().join("");
}
}
const u = new User("secret");
u.checkPassword("secret"); // ok
u.password; // Error - private
u.encryptPassword(); // Error - privateEverything marked private is accessible only inside the class itself
(not from outside, and not in subclasses).
2. Visibility rules
| Where you access it from | Can you access private? |
|---|---|
| Inside the class | Yes |
| In a subclass | No |
| From outside (via an instance) | No |
3. An example with inheritance
class Animal {
private type = "animal";
public showType() {
console.log(this.type);
}
}
class Dog extends Animal {
bark() {
// console.log(this.type); Error - private in Animal
}
}
const dog = new Dog();
dog.showType(); // works through the public method
privateprotects data even from subclasses. If you need it visible in derived classes, useprotected.
4. Private fields in constructor parameters
class User {
constructor(private name: string, private age: number) {}
public getInfo() {
return `${this.name}, ${this.age}`;
}
}
const u = new User("Tim", 30);
u.getInfo(); // Tim, 30
u.name; // Error: privateTypeScript creates and types the private fields itself if you specify private directly in the constructor parameter.
5. The difference between private (TS) and #private (JS)
TypeScript supports two kinds of privacy:
| Kind | Level of privacy | Checked | Available at runtime |
|---|---|---|---|
private | Only at the TypeScript level | at compile time | No (removed in JS) |
#private | A real JavaScript field (ES2022) | by the JS engine | Yes (actually private) |
Example:
class Account {
private balance = 100; // TS-only privacy
#id = 42; // JS-level privacy
getInfo() {
console.log(this.balance, this.#id);
}
}
const a = new Account();
a.balance; // Type error (but accessible at runtime via a["balance"])
a.#id; // Error at both the type level and the JS level - truly hidden#private works even after compilation -
it is real protection at runtime, not just in TS.
6. Combining private + readonly
Sometimes fields are made private and read-only:
class Config {
private readonly secret = "key123";
}The value cannot be changed even inside the class after initialization.
Summary
| Characteristic | Description |
|---|---|
| Accessibility | Only inside the class |
| Access from subclasses | No |
| Access from outside | No |
| Level of protection | Checked by TypeScript at compile time |
| Alternative | #private - a real private field in JS |
| Common use | For hiding logic, passwords, tokens, internal data |
In short:
privateprotects a class's internal data from outside code and makes it accessible only inside the class itself.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.