What are Access Modifiers in TypeScript?
Learn TypeScript access modifiers — public, private, and protected — with clear examples, encapsulation tips, and runtime vs compile-time enforcement explained.
Expected Interview Answer
Access modifiers in TypeScript are keywords — public, private, and protected — that control where a class member (property or method) can be accessed from.
public is the default and allows access anywhere. private restricts a member to the class that declares it. protected allows access within the class and its subclasses. TypeScript also offers readonly to prevent reassignment after initialization, and the newer ECMAScript hash-prefix (#field) for true runtime private fields. Compiler-level modifiers are enforced only at compile time and disappear from the emitted JavaScript.
- Enforces encapsulation of internal state
- Prevents accidental external mutation
- Documents intended API surface
- Enables safer refactoring
- Catches invalid access at compile time
AI Mentor Explanation
Think of a cricket team's dressing room. public is like the scoreboard everyone in the stadium can read, protected is like tactics shared only among the current squad and youth reserves who may be promoted, and private is the captain's personal notebook that never leaves their kit bag — access modifiers set exactly these visibility zones for class members.
Step-by-Step Explanation
Step 1
Default is public
Any member without a modifier is public and reachable from anywhere the instance is available.
Step 2
Mark internals private
Add private to fields and helper methods that should only be touched inside the declaring class.
Step 3
Share with subclasses via protected
Use protected when a subclass legitimately needs the member but external code should not.
Step 4
Lock values with readonly
Combine readonly with a modifier to allow reads but block reassignment after construction.
Step 5
Prefer #private for runtime safety
Use ECMAScript #fields when you need enforcement that survives to the emitted JavaScript, not just the compiler.
What Interviewer Expects
- Knows the three modifiers and their scopes
- Understands public is the default
- Can explain compile-time-only enforcement
- Distinguishes private from #private runtime fields
- Mentions readonly and parameter properties
Common Mistakes
- Believing private prevents access at runtime in emitted JS
- Confusing protected with private
- Thinking readonly is an access modifier controlling visibility
- Forgetting members are public by default
Best Answer (HR Friendly)
“Access modifiers are keywords that decide who can use each part of a class. public means anyone can, private means only that class can, and protected means the class and things built from it can. They keep code organized and prevent accidental misuse.”
Code Example
class BankAccount {
public readonly owner: string
private balance: number
protected accountType: string
constructor(owner: string, opening: number) {
this.owner = owner
this.balance = opening
this.accountType = 'standard'
}
public deposit(amount: number): void {
this.balance += amount
}
public getBalance(): number {
return this.balance
}
}
const acct = new BankAccount('Ada', 100)
acct.deposit(50)
console.log(acct.getBalance()) // 150
// acct.balance -> Error: 'balance' is private
// acct.owner = 'Bob' -> Error: 'owner' is read-onlyFollow-up Questions
- What is the difference between private and the #private field syntax?
- Are access modifiers enforced at runtime?
- How do parameter properties shorten constructors?
- When would you choose protected over private?
- Does readonly affect visibility or mutability?
MCQ Practice
1. Which is the default access modifier in TypeScript?
Members declared without a modifier are public and accessible from anywhere.
2. A protected member can be accessed from where?
protected exposes a member to the declaring class and any classes that extend it, but not external code.
3. Which statement about TypeScript's private modifier is true?
The private keyword is a compile-time check; the emitted JavaScript still exposes the field unless you use #private.
Flash Cards
Default modifier in TypeScript? — public — accessible from anywhere.
Scope of private — Only inside the declaring class.
Scope of protected — The declaring class and its subclasses.
private vs #private — private is compile-time only; #private is enforced at runtime in the emitted JavaScript.
What does readonly do? — Allows reads but blocks reassignment after initialization; it is not a visibility modifier.