Classes & Advanced OOP
Classes live in both worlds
Section titled “Classes live in both worlds”Almost everything else in this course is a type — a compile-time construct that gets erased. A class is different: it is one of the rare declarations that creates both a type and a value. The class name is a type you can annotate with, and simultaneously a runtime constructor you can call with new.
class User { constructor(public name: string) {}}
const u: User = new User("Ada"); // `User` as a TYPE (annotation) and a VALUE (constructor)That dual nature is why classes feel special, and why their features split into two groups: ones the compiler checks and erases (like private), and ones that survive into the emitted JavaScript (like #private fields).
What this module covers
Section titled “What this module covers”| Lesson | What you’ll learn |
|---|---|
| Classes, deep | Access modifiers, readonly, parameter properties, static, abstract, implements |
this types & polymorphism | Polymorphic this, this parameters, and binding pitfalls in the type system |
| Decorators | The modern stage-3 decorators — class, method, field — and factories |
| Mixins & composition | Building behavior from composable class factories instead of deep hierarchies |
A word on inheritance
Section titled “A word on inheritance”TypeScript supports classical extends inheritance, but this module leans toward the features the community actually reaches for in modern code: composition over inheritance. Deep class hierarchies are brittle; this types, decorators, and mixins let you build flexible, reusable behavior without them.
flowchart TB cls["class declaration"] --> typeworld["TYPE world (erased): private, protected, implements, abstract"] cls --> valueworld["VALUE world (runtime): #private, static, the constructor, methods"]