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.
class Account { public owner: string; protected balance = 0; private pin = "0000"; constructor(owner: string) { this.owner = owner; } }
Try it
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.
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
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
protectedmember may be re-declaredpublicin a subclass.
Usage notes
- Compile time only.
Object.keys(a)and plain JavaScript can still seeprivatemembers. They also appear inJSON.stringifyoutput. - 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.pinfor another instance of the same class. - Constructors can be
privateorprotectedtoo, to blocknewfrom outside (singletons, factories). - Parameter properties (
constructor(private d: number)) declare and assign a field in one step;erasableSyntaxOnlyforbids them. - Versus
#private.#xis enforced by the JavaScript engine, cannot be bypassed and is not visible to subclasses.privateis lighter, typed, and works with decorators and older targets. readonlycan be combined:private readonly id. The usual order is visibility,static,abstract/override,readonly.
Related
- #private, readonly, class, Parameter properties
- Lessons: Access modifiers, Classes