Skip to content

Classes

Goal: Encapsulate state with classes — structural conformance, not implements.

public / private modifiers are allowed. Do not use implements — it is a parse error.

Example

class Counter {
  private value: number = 0

  increment(): void {
    this.value = this.value + 1
  }

  read(): number {
    return this.value
  }
}

const c: Counter = new Counter()
c.increment()

Open in playground

Key takeaways

  • Fields are typed like object type members.
  • Structural object types describe required methods.
  • No decorator support.

Next: Modules

Documentation