Class Modifiers
public, private, protected, readonly and static control access. TypeScript private is compile time only, while #name is truly private at runtime. abstract classes define contracts.
Example
class Account {
static count = 0;
readonly id: number;
#balance = 0;
constructor(public owner: string) { this.id = ++Account.count; }
deposit(n: number) { this.#balance += n; return this.#balance; }
}
console.log(new Account("Ada").deposit(50), Account.count);Result
50 1
Exercise
Truly private field marker.
balance = 0;Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.