Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A static method in TypeScript is a function defined on a class that is bound to the class constructor object itself, rather than being attached to the prototype of instantiated objects. Declared using the static keyword, these methods are invoked directly on the class reference and cannot be called on instances of the class.
class Computation {
    static multiply(a: number, b: number): number {
        return a * b;
    }
}

// Invoked directly on the class
const result = Computation.multiply(5, 10); 

// Error: Property 'multiply' does not exist on type 'Computation'.
const instance = new Computation();
instance.multiply(5, 10); 

Execution Context (this)

Within a static method, the execution context (this) references the class constructor, not an instance. Consequently, a static method cannot access instance properties or instance methods. It can only access other static members of the same class.
class StateManager {
    private static stateId: string = "INIT";
    public instanceValue: number = 42;

    static updateState(newId: string): void {
        // Valid: Accessing a static property via 'this'
        this.stateId = newId; 
        
        // Error: Property 'instanceValue' does not exist on type 'typeof StateManager'.
        console.log(this.instanceValue); 
    }
}

Access Modifiers

Static methods fully support TypeScript’s access modifiers (public, private, and protected).
  • public static: (Default) Callable from anywhere the class is accessible.
  • private static: Callable only from within the exact same class. Access is permitted from both static contexts (other static methods or blocks) and instance contexts (constructors or instance methods) within that class by referencing the class name (e.g., ClassName.privateStaticMethod()).
  • protected static: Callable from within the defining class and any derived subclasses. Like private static, access is permitted from both static and instance contexts within these allowed classes.

Inheritance and super

Static methods are inherited by subclasses. A derived class can invoke a parent class’s static method using the super keyword or by directly referencing the parent class name. Derived classes can also override static methods, provided the new method signature is compatible with the parent’s signature.
class BaseEntity {
    protected static initialize(): void {
        console.log("Base initialized");
    }
}

class UserEntity extends BaseEntity {
    public static setup(): void {
        // Invoking inherited static method
        super.initialize(); 
    }

    // Overriding the static method
    protected static initialize(): void {
        console.log("User initialized");
    }
}

Generics in Static Methods

Static methods cannot reference class-level generic type parameters. Because static methods belong to the class constructor and not to any specific instance, the class-level type parameter is unresolved at the time the static method is evaluated. To use generics within a static method, the method must declare its own independent type parameters.
class DataWrapper<T> {
    // Error: Static members cannot reference class type parameters.
    // static process(data: T): void {}

    // Valid: The static method declares its own generic type parameter <U>.
    static process<U>(data: U): U {
        return data;
    }
}
Master TypeScript with Deep Grasping Methodology!Learn More