Skip to content
tezvyn:
Menu

TypeScript Classes: Blueprints for Typed Objects

Source: typescriptlang.orgEasyHow cards are made

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

Interview question

According to the card, what is the "main footgun" that TypeScript's strict mode will flag as an error when defining a class?

  • a.Declaring a class property as readonly without providing an initial value.
  • b.Forgetting to initialize a class property within the constructor or with a default value.Correct
  • c.Defining a class method without explicitly specifying its return type.
  • d.Creating a class instance without using the new keyword.
Why?

The card explicitly states, "The main footgun is forgetting to initialize properties, which strict mode flags as an error," directly referring to the strictPropertyInitialization rule. While declaring a readonly property without an initial value would also be an error, the card highlights the general issue of uninitialized properties as the "main footgun."

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles