Skip to main content

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

javascript
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 - private

Everything marked private is accessible only inside the class itself (not from outside, and not in subclasses).


2. Visibility rules

Where you access it fromCan you access private?
Inside the classYes
In a subclassNo
From outside (via an instance)No

3. An example with inheritance

javascript
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

private protects data even from subclasses. If you need it visible in derived classes, use protected.


4. Private fields in constructor parameters

javascript
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: private

TypeScript 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:

KindLevel of privacyCheckedAvailable at runtime
privateOnly at the TypeScript levelat compile timeNo (removed in JS)
#privateA real JavaScript field (ES2022)by the JS engineYes (actually private)

Example:

javascript
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:

javascript
class Config { private readonly secret = "key123"; }

The value cannot be changed even inside the class after initialization.


Summary

CharacteristicDescription
AccessibilityOnly inside the class
Access from subclassesNo
Access from outsideNo
Level of protectionChecked by TypeScript at compile time
Alternative#private - a real private field in JS
Common useFor hiding logic, passwords, tokens, internal data

In short:

private protects a class's internal data from outside code and makes it accessible only inside the class itself.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.