Classes in TypeScript
Access Modifiers — public, private, protected
Control property and method visibility using public, private, protected, and ES #private syntax.
Last Updated: July 29, 2026
•
10 min read
Access modifiers dictate compile-time and runtime visibility of class members.
1. Introduction & Architecture
2. Deep Dive & Core Concepts
TypeScript private is enforced strictly at compile-time. ES native #field is enforced at runtime by V8 JavaScript engine.
3. Basic Code Example
4. Advanced Production Patterns
5. Interactive Code Playground
class Guarded {
private key = "12345";
#nativeKey = "67890";
}
console.log(new Guarded());
6. Common Pitfalls & Edge Cases
Note: TS
private members can still be accessed via obj["key"] at runtime. Use ES #private if true runtime privacy is required.7. Interview Q&A & Quizzes
Q: What is the difference between private and #field in TypeScript?
A: private is compile-time only (erased in JS). #field is hard runtime private enforced by the JavaScript engine.
8. Summary Comparison Table
| Modifier | Class Access | Subclass Access | External Access | Runtime Privacy |
|---|---|---|---|---|
public | Yes | Yes | Yes | No |
protected | Yes | Yes | No | No |
private | Yes | No | No | No (TS Only) |
#private | Yes | No | No | Hard (JS Native) |