Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript class declarations

Reference for the TypeScript class keyword: typed fields, constructors, methods, accessors, class expressions, strict property initialization and structural typing.

A class declaration creates a constructor function and, at the same time, a type with the same name. The type describes the instance side: the fields and methods every object created with new has. TypeScript adds field declarations with types, member modifiers (public, private, protected, readonly, static, abstract, override) and parameter properties on top of JavaScript classes.

Because TypeScript is structurally typed, a class type matches any object with the same shape, not only instances of that class. Private and protected members are the exception: they make the type nominal-like.

TypeScript
class Point {
  x: number;
  y: number;
  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

Try it

TypeScript Fields, methods and accessors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Strict initialization and structure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
class Name<T> extends Base implements Iface {
  static count = 0;                 // static member
  field: Type = initial;            // instance field
  #secret = 1;                      // JavaScript private field
  constructor(param: Type, public shorthand: Type) { super(); }
  method(arg: Type): Return { /* ... */ }
  get prop(): Type { /* ... */ }
  set prop(v: Type) { /* ... */ }
  static { /* static initialization block */ }
}
const Expr = class { /* class expression */ };
Part Description
Field name: Type with an optional initializer. Fields are own properties of every instance.
constructor At most one implementation (overloads allowed). It cannot declare a return type.
Parameter property A constructor parameter with a modifier (public, private, protected, readonly) becomes a field automatically.
Method / accessor Defined on the prototype; get/set pairs follow special type rules.
Type parameters class Box<T> makes the class generic; static members cannot use them.

Edge cases

  • With strictPropertyInitialization (part of strict), a field with no initializer must be assigned in every constructor path, or you get error TS2564. Use field!: Type only when something else guarantees initialization.
  • A class declaration is not hoisted for use: using it before its line throws a ReferenceError at runtime (and TypeScript reports TS2449).
  • Calling a class without new is an error in JavaScript and in the type system.

Usage notes

  • Two types per class. typeof Counter is the constructor side (with statics); Counter is the instance type.
  • Fields are initialized in order, before the constructor body in a base class and right after super() returns in a derived class.
  • useDefineForClassFields is on by default for ES2022 targets and above; it changes field emit from assignment to define semantics, which matters when a base class sets properties a derived class redeclares.
  • TypeScript private is compile-time only. For real runtime privacy use #name fields.
  • Prefer composition when a class only needs another object's behavior, and interface plus implements when you only need a contract.
  • erasableSyntaxOnly rejects parameter properties, enums and namespaces, which matters when Node strips types without compiling.
  • Decorators on classes use a separate proposal-based system; see the decorator pages.