tezvyn:

TypeScript Classes: Blueprints for Typed Objects

AI-drafted, machine-checkedSource: typescriptlang.orgbeginner

A TypeScript class is a blueprint for creating objects, adding type safety to JavaScript's object-oriented patterns. Use them for core data structures like a `User`. The main footgun is forgetting to initialize properties, which strict mode flags as an error.

WHY IT EXISTS: JavaScript's object creation can be loose and error-prone. TypeScript classes provide a formal, reusable structure for creating objects, ensuring they conform to a specific shape and type contract, which helps catch common bugs before your code ever runs.

THE MENTAL MODEL: Think of a class as a cookie cutter for objects. The class defines the shape (properties like name: string) and features (methods like greet()). Every time you use new, you're stamping out a new cookie (an object instance) that perfectly matches the cutter's design. TypeScript inspects both the cutter and the cookie to enforce type safety.

HOW IT WORKS: You declare a class with the class keyword. Inside, you define members like fields and methods. Fields are properties on the object instance, declared with a name and type (e.g., username: string). The constructor is a special method that runs once when you create a new instance, and it's where you typically initialize the fields. A key TypeScript feature, strictPropertyInitialization, requires all fields to be assigned a value, either with a default initializer (isLoggedIn: boolean = false;) or within the constructor. If a field will be initialized externally, you can use the definite assignment assertion (profile!: Profile;) to tell the compiler to trust you. You can also make fields readonly to prevent them from being changed after the object is constructed.

WHEN TO USE IT: Use classes when you need to create multiple instances of an object that share the same structure and behavior. This is ideal for domain models (e.g., User, Order), service containers in an API (e.g., DatabaseService), or UI components in frameworks like Angular or React.

WHEN NOT TO USE IT: For simple, one-off objects, a plain object literal with a type alias or interface is often simpler. If you just need to group related functions without state, a namespace or a simple object of functions may be better. Overusing classes can lead to unnecessarily complex hierarchies.

ONE CANONICAL EXAMPLE: A class to represent a point in 2D space. It has typed properties, a constructor to initialize them, and a method that performs a calculation. class Point { x: number; y: number;

constructor(x: number = 0, y: number = 0) { this.x = x; this.y = y; }

distanceFromOrigin() { return Math.sqrt(this.x * this.x + this.y * this.y); } }

const p1 = new Point(3, 4); console.log(p1.distanceFromOrigin()); // Prints 5

Read the original → typescriptlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.