Course contentsShow
← TypeScript
Lesson 1060 of 1,79624. Classes in TypeScriptFree lesson

Typing Static Members

Understand how TypeScript infers and checks types for static properties and methods on classes.

Typing Static Members

You'll learn: how TypeScript infers, checks, and lets you explicitly type properties and methods that belong to the class itself rather than to instances.

What Are Static Members, Type-Wise?

Recall that static members live on the class constructor function itself, not on instances. TypeScript treats static properties and methods as having their own distinct types, completely separate from instance members.

When you write static age: number, TypeScript understands that MyClass.age is a number, but instances (new MyClass()) don't have age at all. The type system enforces this distinction at compile time.

Type Inference for Static Members

Just like instance properties, TypeScript infers types from initializers:

class Config {
  static version = "1.0.0";        // inferred as string
  static maxRetries = 3;           // inferred as number
  static features = ["auth", "db"]; // inferred as string[]
}

// TypeScript knows these types:
const v: string = Config.version;
const m: number = Config.maxRetries;

Explicit Type Annotations

You can (and often should) annotate static members explicitly for clarity and safety:

class Database {
  static instanceCount: number = 0;
  static defaultTimeout: number;
  
  constructor() {
    Database.instanceCount++;
  }
}

Database.defaultTimeout = 5000; // Must assign before use

TypeScript enforces that assignments match the declared type, just like with instance properties.

Static Methods and this

In static methods, this refers to the constructor function, and TypeScript types it accordingly:

class Counter {
  static count: number = 0;
  
  static increment(): void {
    this.count++; // this is typed as typeof Counter
  }
  
  static getCount(): number {
    return this.count;
  }
}

Inside a static method, this has the type typeof ClassName, allowing type-safe access to other static members.

Why Typing Static Members Matters

Static members often hold configuration, caches, or factory logic. Typing them prevents bugs like assigning the wrong value type to Config.timeout or calling a static method with incorrect arguments. The type system treats the class constructor as its own distinct typed object.

Key Takeaway: TypeScript infers and checks static member types just like instance members, but treats them as properties of the class constructor itself—enforcing that static and instance namespaces remain completely separate at the type level.