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.
class User { name: string; nickname?: string; readonly createdBy = "system"; constructor(name: string) { this.name = name; } }
Try it
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.
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
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 excludesundefinedneeds an initializer or an assignment in every constructor path. readonlyfields can be assigned only in the declaration or in the constructor.declare name: Typeemits no code; it only tells the checker that a base class or other code creates the field.
Usage notes
- With
targetES2022 or later (and by default foruseDefineForClassFields), fields use define semantics: they are created withObject.defineProperty-like behavior, even if no initializer is given. A field written asx?: numberbecomes an own property set toundefined. - Because of define semantics, a derived class that redeclares a base-class field without an initializer resets it to
undefined. Usedeclareto 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 = () => {}) bindthisper 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 runtimeundefined.