1. Introduction
Access modifiers let you control which parts of a codebase are allowed to read or write a class member. TypeScript supports three modifiers — public, private, and protected — that describe how a property or method can be accessed from outside the class or from subclasses.
Cricket analogy: A team's public press-conference quotes are open to anyone, but a batter's private pre-match rituals are seen by no one, while protected dressing-room strategy is shared only with teammates and captains, not the press.
2. Syntax
class ClassName {
public publicField: Type; // accessible everywhere (default)
private privateField: Type; // accessible only within this class
protected protectedField: Type; // accessible within this class and subclasses
private privateMethod(): void {}
}3. Explanation
public is the default modifier — members are accessible from anywhere if no modifier is written. private restricts access to inside the declaring class only, not even subclasses can see it. protected is similar to private but also allows access from subclasses, which is useful when a base class needs to expose internals to derived classes without making them fully public.
Cricket analogy: By default a player's stats are public to anyone; marking data private means not even the vice-captain (a subclass leader) can see it, while protected data is visible to the captain and vice-captain but no one outside leadership.
These modifiers are purely a compile-time construct enforced by the TypeScript type checker. They help catch accidental misuse of internal state during development and give editors better autocomplete, but they do not change how the code actually runs.
Cricket analogy: A scorer marking a stat as 'internal use only' on the paper scoresheet doesn't stop anyone physically reading it during the match — it's a convention enforced only during scorecard review, not during actual play.
public, private, and protected are erased completely when TypeScript compiles to JavaScript — the emitted class has ordinary properties with no runtime enforcement, so a private field can still be read or written from outside using type-unsafe tricks like (obj as any).field. If you need real runtime-enforced privacy, use ES2022 native private fields with a '#' prefix (e.g. #balance), which the JavaScript engine itself refuses to expose outside the class.
4. Example
class BankAccount {
public accountHolder: string;
private balance: number;
protected accountType: string;
constructor(accountHolder: string, balance: number) {
this.accountHolder = accountHolder;
this.balance = balance;
this.accountType = "Savings";
}
public deposit(amount: number): void {
this.balance += amount;
console.log(`Deposited ${amount}. New balance: ${this.balance}`);
}
private showBalance(): number {
return this.balance;
}
public getBalance(): number {
return this.showBalance();
}
}
const acc = new BankAccount("Riya", 1000);
acc.deposit(500);
console.log(acc.getBalance());
console.log((acc as any).balance); // bypasses TS check, still works at runtime5. Output
Deposited 500. New balance: 1500
1500
15006. Key Takeaways
- public is the default modifier and allows access from anywhere.
- private restricts access to the declaring class only.
- protected allows access within the class and its subclasses, but not from outside.
- All three modifiers are compile-time only and are erased from the emitted JavaScript.
- For true runtime-enforced privacy, use ES2022 '#' private fields instead of the 'private' keyword.
Practice what you learned
1. Which access modifier is applied to a class member if none is written explicitly?
2. Why does `(acc as any).balance` successfully print 1500 in the example even though balance is private?
3. What is the key difference between private and protected members?
4. If you need a class field that JavaScript itself refuses to expose outside the class at runtime, what should you use instead of the 'private' keyword?
5. In the BankAccount example, what does acc.getBalance() return?
Was this page helpful?
You May Also Like
Classes in TypeScript
Learn how TypeScript classes add type-checked properties, methods, and constructor parameter property shorthand on top of ES2015 classes.
Readonly Properties in TypeScript
Use the readonly modifier to prevent a class property from being reassigned after it is initially set in the constructor or at declaration.
Static Members in TypeScript
Define properties and methods that belong to the class itself rather than to individual instances, using the static keyword.
Getters and Setters in TypeScript
Use get and set accessors to expose class properties that run custom logic, such as validation, while still being accessed with plain property syntax.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics