Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript public, private and protected modifiers

Reference for TypeScript member visibility: public, private and protected, parameter properties, how checks work at compile time only, and how they differ from #private.

TypeScript has three visibility modifiers for class members. public (the default) means anyone can use the member. protected means the class and its subclasses can. private means only code inside the same class declaration can.

These checks happen only while compiling. After type erasure, a private field is an ordinary property that JavaScript code can read freely. When you need real runtime privacy, use a #name field instead.

TypeScript
class Account {
  public owner: string;
  protected balance = 0;
  private pin = "0000";
  constructor(owner: string) { this.owner = owner; }
}

Try it

TypeScript Who can see what

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Violating visibility

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
class C {
  public a = 1;               // same as no modifier
  protected b = 2;
  private c = 3;
  constructor(private d: number) {}   // parameter property
  private static s = 4;
  protected method() {}
}
Modifier Accessible from
public Everywhere. Default for all members.
protected The declaring class and its subclasses (through an instance of the subclass, not the base type). Not from outside.
private Only the declaring class body (including other instances of the same class). Not subclasses.

Errors

  • Outside access to protected: error TS2445 ("Property 'balance' is protected and only accessible within class 'Account' and its subclasses").
  • Outside access to private: error TS2341 ("Property 'pin' is private and only accessible within class 'Account'"); a subclass reaching it gets the same message.
  • A subclass cannot make an inherited member less visible, and a protected member may be re-declared public in a subclass.

Usage notes

  • Compile time only. Object.keys(a) and plain JavaScript can still see private members. They also appear in JSON.stringify output.
  • Bracket notation (a["pin"]) is allowed as an explicit escape hatch; it is useful in tests.
  • Nominal side effect. A class with a private or protected member is only assignable from that class (or its subclasses). Two classes with identically named private fields are not interchangeable.
  • Same-class access. A method can read other.pin for another instance of the same class.
  • Constructors can be private or protected too, to block new from outside (singletons, factories).
  • Parameter properties (constructor(private d: number)) declare and assign a field in one step; erasableSyntaxOnly forbids them.
  • Versus #private. #x is enforced by the JavaScript engine, cannot be bypassed and is not visible to subclasses. private is lighter, typed, and works with decorators and older targets.
  • readonly can be combined: private readonly id. The usual order is visibility, static, abstract/override, readonly.