Last updated: Apr 29, 2026

Classes

Quick-return cheat sheet for TypeScript classes — syntax, modifiers, abstract classes, decorators, and gotchas.

Common Syntax

class App extends Base implements AppConfig, Serializable {
  env: string; // field
  displayName?: string; // optional field
  name!: string; // definite assignment (trust me, TS)
  #secrets: Map<string, string>; // runtime private
  roles = ["user"]; // default value
  readonly createdAt = new Date(); // readonly with default

  constructor(env: string, host: string) {
    super(host);
    this.env = env;
  }

  setName(name: string) {
    this.name = name;
  } // method
  verifyName = (name: string) => {
    /* ... */
  };

  // overloads
  sync(): Promise<{ ok: boolean }>;
  sync(cb: (result: string) => void): void;
  sync(cb?: (result: string) => void): void | Promise<{ ok: boolean }> {
    /* ... */
  }

  get version() {
    /* ... */
  } // getter
  set version(value: string) {
    /* ... */
  } // setter

  private makeRequest() {
    /* ... */
  } // type-only private
  protected handleRequest() {
    /* ... */
  } // accessible to subclasses

  static #instanceCount = 0; // static private field
  static create(env: string) {
    /* ... */
  } // static method
  static {
    this.#instanceCount = -1;
  } // static block
}

Parameter Properties

Prefixing a constructor parameter with public, protected, private, or readonly automatically creates and assigns a matching instance field.

class Endpoint {
  constructor(
    public host: string,
    public port: number,
    private readonly secret: string,
  ) {}
}

const ep = new Endpoint("localhost", 3000, "abc");
ep.host; // "localhost"
ep.port; // 3000

Abstract Classes

abstract class Transport {
  abstract send(data: string): Promise<void>;
  log(msg: string) {
    console.log(`[${this.constructor.name}] ${msg}`);
  }
}

class HttpTransport extends Transport {
  async send(data: string) {
    /* ... */
  }
}

Decorators

import { sealed, log, validate } from "mylib";

@sealed
class App {
  @log
  start() {
    /* ... */
  }

  @validate
  set config(value: string) {
    /* ... */
  }
}