Chapter 14: TypeScript Classes

TypeScript Classes like we’re sitting in a quiet room with a whiteboard, coffee, and no hurry. I’ll explain everything slowly, show patterns people actually use in 2025–2026 real projects, highlight what’s safe / common / dangerous, and give many practical examples.

1. Quick reality check first (very important)

TypeScript classes = JavaScript classes + type annotations + access modifiers + a few extra checks

  • At runtime → exactly the same as normal ES2015+ classes (no extra code for private fields in most cases)
  • TypeScript adds:
    • Type checking for properties & methods
    • public / private / protected (some become real #private at runtime)
    • readonly
    • Parameter properties (shorthand)
    • Implements / extends with interface checks
    • Better constructor / field initialization rules (especially with strictPropertyInitialization)

2. The most basic class (start here)

TypeScript

3. Parameter properties — very common shorthand (you’ll see this everywhere)

Instead of writing this.xxx = xxx manually:

TypeScript

Access modifiers quick table (2025–2026 reality)

Modifier Accessible from Real JS output (most targets) Common use case
(no keyword) everywhere normal property Default — public API
public everywhere same Explicit public (some teams prefer)
private only inside class #private (when target ≥ es2022) or WeakMap Internal state
protected class + subclasses #protected or WeakMap Inheritance — give to children
readonly read, but no re-assign readonly in .d.ts IDs, configs, immutable fields

4. Inheritance (extends) — classic OOP pattern

TypeScript

5. Abstract classes (very useful for defining contracts)

TypeScript

6. Implementing interfaces (very common in real apps)

TypeScript

Many real projects use multiple implements — especially domain entities + DTOs.

7. Static members (belong to class, not instance)

TypeScript

8. Modern patterns you see in 2025–2026 codebases

Pattern A: Private fields + getters/setters

TypeScript

Pattern B: Readonly + constructor assignment

TypeScript

Pattern C: Using classes with React (classic class component style — still exists)

tsx

9. Strict mode gotchas (very important — enable strict!)

With “strict”: true or “strictPropertyInitialization”: true:

TypeScript

Many teams use ! sparingly — prefer default values or constructor init.

Quick cheat-sheet table

Feature Syntax example Purpose / When to use
Parameter property constructor(public name: string) Reduce boilerplate — very common
Private field private balance: number or #balance Hide internal state
Protected protected log() Share with subclasses only
Readonly readonly id: string Prevent accidental mutation
Abstract class abstract class Shape { abstract area(): number } Define base contract
Static static createDefault() Factory methods, constants
Implements class User implements IUser Enforce shape (very frequent with interfaces)
Getter / Setter get fullName() {} Computed properties

Your mini homework (try in playground)

  1. Create class Employee with public name, private salary, protected department
  2. Add raiseSalary(percent: number) method that only works inside class
  3. Create class Manager extends Employee that can see department
  4. Make salary readonly after constructor

Any part confusing? Want to zoom in on:

  • Private vs #private differences
  • Abstract classes + templates
  • Classes vs interfaces (when to choose what)
  • Real React class vs function components with types
  • Common mistakes people still make in 2026

Just say the word — we’ll go deeper right there! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *