TypeScript Reference ยท Keywords and Declarations
TypeScript static members and static blocks
Reference for static in TypeScript classes: static fields, methods, accessors, static initialization blocks, inheritance of statics and common mistakes.
A static member belongs to the class itself instead of to its instances. You reach it through the class name (Config.defaults), not through an object created with new. Static fields, methods and accessors are common for counters, factories, constants and registries.
A static { ... } block runs once when the class is defined, in textual order with static field initializers. It is the place for setup that needs several statements or try/catch.
class Id { static next = 1; static make(): number { return Id.next++; } }
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 { static field: Type = initial; static method(): R { /* ... */ } static get prop(): T { /* ... */ } static #hidden = 1; // private static static { /* initialization block */ } }
| Part | Description |
|---|---|
static field |
One shared value stored on the constructor. May combine with readonly, private, protected, public. |
static method |
Callable without an instance. Inside it this is the class (or the subclass it was called on). |
static { } |
Class static block (ES2022; TypeScript 4.4+). Runs in order with static fields. Can use try/catch, loops and local variables. |
Errors and edge cases
- Reading a static member through an instance (
t.freezing): error TS2576 ("Property 'freezing' does not exist on type 'Temperature'. Did you mean to access the static member 'Temperature.freezing' instead?"). - A static member that uses a class type parameter: error TS2302 ("Static members cannot reference class type parameters").
- Static properties named
name,lengthorprototypecan clash with built-in properties of constructor functions, and some are rejected by the compiler. Pick other names.
Usage notes
- Inheritance. A subclass inherits statics through its prototype chain:
Child.sizeworks, but a static field assigned in the parent is shared, andChild.count++creates an own property onChild. thisin static code. Writingthis.countmakes the code follow the class it is called on; writingRegistry.countalways uses the parent's. Pick deliberately.- Initialization order. Static fields and blocks run top to bottom when the class is evaluated, before any instance exists.
- Not for grouping functions. A class holding only static members is often better as a module with plain exports or a
constobject. - Testing and globals. Static mutable state is shared across the whole program, so tests may need to reset it.
abstractandstaticcannot be combined. Static members cannot be abstract.- Static private members use
private static(compile-time) orstatic #x(runtime).