100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript

Access Modifiers in TypeScript (public, private, protected)

Control the visibility of class members with public, private, and protected, and understand that these checks exist only at compile time.

Classes & OOPIntermediate8 min readJul 8, 2026
Analogies

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

typescript
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

typescript
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 runtime

5. Output

text
Deposited 500. New balance: 1500
1500
1500

6. 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

Was this page helpful?

Topics covered

#TypeScript#TypeScriptProgrammingStudyNotes#Programming#AccessModifiersInTypeScriptPublicPrivateProtected#Access#Modifiers#Public#Private#StudyNotes#SkillVeris