Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Classes, Enums and Decorators

TypeScript class fields

Reference for TypeScript class fields: declare typed properties, initializers, optional and definite assignment, readonly, and strictPropertyInitialization.

A class field is a property declared in the body of a class. Each instance gets its own copy, set up when the object is constructed. In TypeScript a field can carry a type annotation, an initializer, and modifiers such as readonly, private or static.

Under --strict, TypeScript also checks that every field is definitely assigned by the end of the constructor (the strictPropertyInitialization rule). Fields that may legitimately be missing must say so in their type.

TypeScript
class User {
  name: string;
  nickname?: string;
  readonly createdBy = "system";
  constructor(name: string) {
    this.name = name;
  }
}

Try it

TypeScript Declaring and initializing fields

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 Optional, definite assignment and strict checks

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 C {
  [modifiers] name[?|!][: Type] [= initializer];
}
Part Description
Modifiers public (default), protected, private, static, readonly, override, declare, accessor.
name An identifier, a string or number literal, a computed name, or a #private name.
? Optional: the type becomes T | undefined and the field need not be assigned.
! Definite assignment assertion: you promise the field is set before it is read.
: Type Optional when an initializer lets the compiler infer the type.
= initializer Runs for each instance, before the constructor body (after super() in derived classes).

Edge cases:

  • With strictPropertyInitialization (part of --strict), a field whose type excludes undefined needs an initializer or an assignment in every constructor path.
  • readonly fields can be assigned only in the declaration or in the constructor.
  • declare name: Type emits no code; it only tells the checker that a base class or other code creates the field.

Usage notes

  • With target ES2022 or later (and by default for useDefineForClassFields), fields use define semantics: they are created with Object.defineProperty-like behavior, even if no initializer is given. A field written as x?: number becomes an own property set to undefined.
  • Because of define semantics, a derived class that redeclares a base-class field without an initializer resets it to undefined. Use declare to avoid that.
  • A field initializer cannot see constructor parameters; use parameter properties or assign in the constructor when you need them.
  • Arrow-function fields (handler = () => {}) bind this per instance but cost one function object per instance instead of sharing a prototype method.
  • Prefer inferred types for simple initializers and write annotations when the initial value is narrower than the intended type, such as items: string[] = [].
  • ! silences the checker; if the promise is wrong you get a runtime undefined.