Docs
/
TypeScript
Chapter 10
10 — Classes & Access Modifiers
Core Concepts
- public — accessible from anywhere (default)
- private — accessible only within the class
- protected — accessible within the class and subclasses
- readonly — can only be set in constructor or declaration
- abstract classes — can't be instantiated, define contracts for subclasses
- implements — class must satisfy an interface
- static members — belong to the class, not instances
- Parameter properties — shorthand constructor
constructor(public name: string) - ES2022
#private— true runtime private fields (vs TSprivatewhich is compile-time only)
Access Modifier Summary
| Modifier | Class | Subclass | Outside |
|---|---|---|---|
public | Yes | Yes | Yes |
protected | Yes | Yes | No |
private | Yes | No | No |
#field | Yes | No | No (runtime enforced) |