TypeScript

Static vs Instance Methods in TypeScript

Static vs instance methods in TypeScript: what each one is, when to use which, and the practical difference, with clear class examples.

23 Apr 2024

Static vs Instance Methods in TypeScript

Two ways to put a method on a class. The difference matters.

Static methods — belong to the class itself

You call them on the class, not on an instance. No new keyword needed.

Text
class MathUtil {
  static add(a: number, b: number): number {
    return a + b;
  }
}

console.log(MathUtil.add(2, 3)); // 5

Static methods don't have access to this (the instance). They can't read instance properties. They're pure utility functions that happen to live on a class.

Instance methods — belong to each object

You call them on an instance created with new. They have access to this and can read/write instance data.

Text
class Person {
  private name: string;

  constructor(name: string) {
    this.name = name;
  }

  sayHello(): void {
    console.log(`Hello, my name is ${this.name}!`);
  }
}

const person = new Person('John');
person.sayHello(); // Hello, my name is John!

When to use which

Static methods for utility functions that don't need instance state — math helpers, factory methods, validators. Think Date.now() or Array.isArray().

Instance methods for behavior tied to a specific object's data — a user's getFullName(), an order's calculateTotal().

The trade-off

Static methods are simpler and easier to test (no setup, no instance creation). But they can't be overridden by subclasses in useful ways, and overusing them leads to procedural code wrapped in class syntax. If your class is all static methods, you don't need a class — use standalone functions instead.

Keep reading