How to Create a Class in TypeScript

Use a class in TypeScript when related data and behavior should stay together in one reusable object blueprint. Classes are perfect for domain models, service objects, UI controllers, and stateful business logic.

What you’ll build or solve

You’ll learn how to create a class in TypeScript with typed properties, constructors, and methods. You’ll also know when classes are better than plain objects.

When this approach works best

This approach is the right choice when multiple instances should share the same structure and methods.

Common real-world scenarios include:

  • User models
  • Cart managers
  • API service clients
  • Game entities
  • Form controllers

This is a bad idea when a simple data shape or utility object would be enough.

Prerequisites

You only need:

  • Basic TypeScript types
  • Familiarity with functions and objects
  • Understanding of constructors

Step-by-step instructions

Step 1: Define the class and typed properties

A class groups properties and methods.

class User {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

This creates a reusable blueprint.

Create an instance with new.

const user = new User("Alex", 28);

This ensures every instance has the same typed shape.

Step 2: Add methods

Methods define reusable behavior.

class User {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  greet(): string {
    return `Hello, ${this.name}`;
  }
}

Now every instance can call the same logic.

user.greet();

Step 3: Use access modifiers for safer design

TypeScript supports public, private, and protected.

class BankAccount {
  private balance: number;

  constructor(balance: number) {
    this.balance = balance;
  }

  getBalance(): number {
    return this.balance;
  }
}

This protects internal state from direct external mutation.

What to look for:

  • Classes define reusable blueprints
  • Constructors initialize state
  • Methods add shared behavior
  • Access modifiers improve safety
  • Use new to create instances

Examples you can copy

Product model

class Product {
  constructor(public name: string, public price: number) {}
}

Cart manager

class Cart {
  addItem() {}
}

Game character

class Player {
  attack() {}
}

Common mistakes and how to fix them

Mistake 1: Forgetting this

What the reader might do:

name = name;

Why it breaks: the class property never updates.

Corrected approach:

Use this.name.

Mistake 2: Missing constructor types

What the reader might do:

constructor(name, age)

Why it breaks: parameters lose type safety.

Corrected approach:

Type every constructor parameter.

Mistake 3: Using classes for plain static data

What the reader might do:

Wrap simple config objects in classes.

Why it breaks: this adds unnecessary ceremony.

Corrected approach:

Use interfaces or plain objects for simple shapes.

Troubleshooting

If properties stay undefined, verify constructor assignments.

If methods cannot access data, use this.

If external code mutates sensitive fields, add private.

If the model has no behavior, consider plain objects instead.

Quick recap

  • Classes combine data and behavior
  • Constructors initialize typed state
  • Methods add reusable logic
  • Use access modifiers for safety
  • Prefer plain objects for simple data-only shapes