A static field in TypeScript is a class-level property bound directly to the class constructor object rather than to individual instances of the class. Declared using theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
static keyword, these fields share a single memory allocation. While static fields are excluded from the instance prototype chain (Class.prototype), they are included in the constructor’s prototype chain (e.g., Object.getPrototypeOf(Derived) === Base), which facilitates static property inheritance across subclasses.
Syntax and Access
Static fields are declared within the class body and are accessed via the class constructor object, rather than an instance reference.Access Modifiers and Mutability
Static fields fully support TypeScript’s access modifiers (public, private, protected) as well as the readonly modifier. These modifiers dictate visibility and mutability strictly at the class level.
private static: Accessible only within the declaring class.protected static: Accessible within the declaring class and its subclasses.static readonly: Can only be assigned during declaration or within a static initialization block.
Execution Context and this
The behavior of the this keyword changes depending on the execution context relative to static fields:
- Inside Static Methods:
thisrefers to the class constructor object. You can usethis.fieldNameto access static fields. - Inside Instance Methods:
thisrefers to the class instance. To access static fields, the most idiomatic approach is to use the explicit class name (ClassName.fieldName). Alternatively, static fields can be accessed dynamically via the instance’s constructor.
Generics and Type Erasure
Static fields cannot reference class type parameters. Because TypeScript’s generic types are erased at runtime, only a single class constructor object exists in memory for all generic instantiations (e.g.,Container<string> and Container<number> share the exact same Container constructor).
Reserved Property Names
Because class constructors are standard JavaScript functions under the hood, static fields cannot use reserved function property names. Declaring a static field with names such asname, length, or call will result in a compilation error.
Inheritance and Shadowing
Static fields are inherited by subclasses via the constructor prototype chain. A subclass can access the parent’s static fields directly. If a subclass declares a static field with the exact same name, it shadows the parent’s static field without mutating the parent’s value.Static Initialization Blocks
For complex initialization logic that cannot be handled by inline assignment, TypeScript supports static initialization blocks (static {}). These blocks execute exactly once when the class is evaluated by the JavaScript engine.
Master TypeScript with Deep Grasping Methodology!Learn More





