Class
How TypeScript extends JavaScript classes with types, access modifiers, and compile-time checks.
TypeScript classes behave the same as JavaScript classes at runtime the extra syntax (types, modifiers, interface implementation) only exists at compile time and is stripped away when compiled down to JS.
For
public/private/protectedandabstractclasses, see Basics.
Typed properties and methods
Parameter properties
TypeScript lets you declare and initialize a property directly from the constructor's parameter list, skipping the separate declaration + assignment:
readonly properties
readonly properties can be set once (in the declaration or the constructor) and never reassigned after that:
implements
A class can implement one or more interfaces, which forces it to satisfy that shape. Unlike extends, implements shares no implementation only the contract:
override
The override keyword documents and lets the compiler verify that a method is intentionally overriding a base class method. It catches typos where a method was meant to override but doesn't actually match the base signature:
Generic classes
Classes can be generic, so the same class can operate over different types safely:
Classes are also types
Declaring a class also declares a type of the same name, usable anywhere a type is expected: