Classes in TypeScript

From the TypeScript cheat sheet ยท Interfaces & Classes ยท verified Jul 2026

Classes

Object-oriented programming with TypeScript classes

typescript
// Basic class
class Person {
  name: string
  age: number
  
  constructor(name: string, age: number) {
    this.name = name
    this.age = age
  }
  
  greet(): string {
    return `Hello, I'm ${this.name}`
  }
}

// Access modifiers
class Employee {
  public name: string
  private salary: number
  protected department: string
  readonly id: number
  
  constructor(name: string, salary: number) {
    this.name = name
    this.salary = salary
    this.id = Math.random()
  }
}
๐Ÿ’ก Use parameter properties to reduce boilerplate
๐Ÿ”’ Private fields start with # in modern TS
๐Ÿ“Œ Abstract classes cannot be instantiated

Continue with TypeScript

Save the full cheat sheet or work through every related task.

More TypeScript tasks

Back to the full TypeScript cheat sheet