TypeScript Classes

A guide to TypeScript classes, constructors, access modifiers, and inheritance with practical examples.

TL;DR

  1. 01Create classes with typed properties and a constructor.
  2. 02Control access with public, private, and protected modifiers.
  3. 03Extend classes to share behavior through inheritance.

Tips

  1. 01Prefer parameter properties in constructors to cut boilerplate, since they declare and assign in a single line.

Warnings

  1. 01Use the <code>#</code> prefix for true privacy when needed, since the <code>private</code> keyword is only enforced at compile time.

Basic Syntax

  • Declare a class with the class keyword and a PascalCase name.
    class User {
      name: string;
      constructor(name: string) {
        this.name = name;
      }
    }
  • Declare typed properties at the top of the class body for clarity.
  • The constructor runs automatically when you create a new instance.
    const sam = new User("Sam");
  • Use this inside methods to refer to the current instance.
  • Methods are typed just like regular functions with parameters and return types.

Constructor Shortcuts

  • Add modifiers to constructor parameters to create and assign properties at once.
    class User {
      constructor(public name: string, public age: number) {}
    }
  • This shortcut saves you from writing property declarations separately.
  • Combine with readonly to make the property immutable after construction.
    class Point {
      constructor(readonly x: number, readonly y: number) {}
    }
  • Use default values in the constructor for optional inputs.
  • Parameter properties work with public, private, protected, and readonly.

Access Modifiers

  • Use public for properties that anyone can read or change.
    class User {
      public name: string = "Sam";
    }
  • Use private to hide properties from code outside the class.
    class Account {
      private balance: number = 0;
    }
  • Use protected to allow access from the class and its subclasses only.
  • The # prefix gives true runtime privacy that survives compilation.
    class Counter {
      #count = 0;
      next() { return ++this.#count; }
    }
  • Default modifier is public when none is specified.

Inheritance

  • Use the extends keyword to create a subclass of another class.
    class Animal {
      move() { console.log("moving"); }
    }
    class Dog extends Animal {
      bark() { console.log("woof"); }
    }
  • Call super() inside the constructor to run the parent constructor first.
    class Cat extends Animal {
      constructor(public name: string) {
        super();
      }
    }
  • Override parent methods by redeclaring them in the subclass.
  • Use super.method() to call the parent method from inside an override.
  • A class can extend only one parent — use interfaces for multiple contracts.

Abstract and Static

  • Mark a class abstract to prevent direct instantiation.
    abstract class Shape {
      abstract area(): number;
    }
  • Subclasses must implement every abstract method to be usable.
  • Use static to attach properties or methods to the class itself, not instances.
    class MathHelper {
      static double(n: number) { return n * 2; }
    }
    MathHelper.double(5); // 10
  • Static methods are great for utility functions related to the class.
  • Use implements to require a class to match an interface contract.

FAQ