Skip to content

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 · 2 min read

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.

Typescript
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.

Typescript
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.

Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.

Go further
Live cohort on Maven

Forward Deployed AI Engineering: From Customer Problem to Production Value

A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.

January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.

View the live cohort

Keep reading