TypeScript Tutorial
TypeScript Access Specifiers
public, private, and protected control who can read and write members.
Who can touch a member
Access specifiers sit on each field and method. public members are open to any code that has an object. private members are only for methods (and constructors) of that class.protected sits between those two when inheritance is in play.
Put data you want to protect in private, and the functions other files should call in public. Unlike C++, TypeScript does not use public: section labels. You write the word on the member itself.
public
Public members are the class's outer surface. The rest of the file can read and write them. That was useful while you learned dots and objects. It is a poor default for real fields: any line of code can setscore = -9 and break a rule the class was supposed to keep.
Example
class Meter {
public reading: number = 0;
public show(): void {
console.log(this.reading);
}
}
const m: Meter = new Meter();
m.reading = 7;
m.show();Members with no specifier are already public. Writing public makes the choice visible. This tutorial keeps the word on purpose.
private
Private members are invisible outside the class. The rest of the file cannot write v.code. It must call a public method such as setCode. Methods inside Vault can still usethis.code directly.
Example
class Vault {
private code: number = 0;
public setCode(c: number): void {
this.code = c;
}
public getCode(): number {
return this.code;
}
}
const v: Vault = new Vault();
v.setCode(42);
console.log(v.getCode());Paste this into /typescript/try, then add v.code = 0; after new Vault()and compile. tsc should reject it: code is private.
public vs private
| public | private | |
|---|---|---|
| Methods of the same class | Can read and write | Can read and write |
| Code outside the class | Can read and write | Cannot; compile error |
| Typical use | Functions other code should call | Fields and helpers |
| Class default | Yes — omit the word and it is still public | No — you must write private |
A class with no specifier at all treats members as public. That is why these lessons writeprivate the moment a field should stay inside the type.
protected
protected is like private for code outside the family of classes, but a derived class can use those members. You need extends to see the difference. Until the inheritance chapter, use public for the interface and private for the data.
Example
class Account {
protected balance: number = 0;
public getBalance(): number {
return this.balance;
}
}
class Savings extends Account {
public deposit(n: number): void {
this.balance = this.balance + n;
}
}
const s: Savings = new Savings();
s.deposit(20);
console.log(s.getBalance());Savings may write this.balance because the field is protected.s.balance = 0 from outside both classes would not compile. Do not mark fields protected just to reach them from the top of the file. That is not what protected is for.
A constructor can set private fields
Constructors belong to the class, so they may assign private members. That is the clean way to start aVault with a code: pass it into the constructor, store it in the private field, never expose the field itself.
Example
class Vault {
private code: number;
constructor(c: number) {
this.code = c;
}
public getCode(): number {
return this.code;
}
}
const v: Vault = new Vault(42);
console.log(v.getCode());Hide the data, publish the operations
Access specifiers are the first half of encapsulation. The next chapter puts that into a habit: private fields, public getters and setters, and rules the class enforces in one place.