Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Language, Modules and Emit Options

TypeScript useDefineForClassFields option

Reference for useDefineForClassFields: define versus set semantics for class fields, the ES2022 default, setter shadowing and fields redeclared in subclasses.

useDefineForClassFields chooses how class fields are initialized in the emitted JavaScript. With the option on, a field x = 1 becomes a real class field (the JavaScript standard behavior), which creates the property with Object.defineProperty semantics. With it off, TypeScript emits an assignment this.x = 1 inside the constructor, the older behavior.

The default is true when target is ES2022 or higher (including ESNext), and false otherwise. The two modes differ in three places: setters and accessors on a base class, fields that are declared but never initialized, and the order in which initializers run.

JSON
{
  "compilerOptions": {
    "target": "ES2022",
    "useDefineForClassFields": true
  }
}

Try it

TypeScript A field shadows a base class setter

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 A redeclared field resets to undefined

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

JSON
{ "compilerOptions": { "useDefineForClassFields": true } }
Part Description
Value true or false. Default: true for target ES2022 or later, otherwise false.
true Fields are emitted as class fields (x = 1; or just x;), defined with [[Define]] semantics. A field without an initializer is created as undefined.
false Initializers become this.x = 1 in the constructor ([[Set]]), and a field with no initializer emits nothing.
Escape hatch declare x: T; never emits anything, in either mode.

Edge cases: with define semantics, an initializer can no longer rely on parameter properties being assigned first; the compiler reports TS2729 ("Property used before its initialization") in obvious cases.

Usage notes

  • Keep it true for ES2022 targets. It matches what the JavaScript engine does with the TypeScript-free source, so what you debug is what you wrote.
  • Redeclaring a field in a subclass is the classic breakage when migrating: the subclass field overwrites a value the base constructor set. Use declare for type-only refinements.
  • Setters and accessors. A subclass field named like a base accessor hides the accessor under define semantics. The compiler reports it as error TS2610, and declare does not silence it; override the accessor with another accessor instead.
  • Frameworks that assign in constructors (older decorator-based libraries) may need false. Check their documentation before switching.
  • Property order and Object.keys can differ between modes for classes with uninitialized fields: define mode creates all of them up front.
  • Standard decorators work with either value; legacy decorators on fields often assume false.