public, private, protected

Access modifiers controlling visibility of class members.

Since TS 1.0 Spec ↗

Syntax

public | private | protected memberName

Examples

class Account {
  public id: number;
  private balance = 0;
  protected owner: string;

  constructor(id: number, owner: string) {
    this.id = id;
    this.owner = owner;
  }
}
class Service {
  constructor(private readonly url: string) {}
  // parameter property creates and assigns this.url
}

Notes

`public` is the default. `private` restricts access to the declaring class; `protected` also allows subclasses. These are compile-time checks; for true runtime privacy use ECMAScript `#` fields. Constructor parameter properties declare and initialize members in one step.

See also