TypeScript Interfaces

TypeScript interfaces are another way to name an object shape. They are especially popular for describing data models, API responses and the public contract of classes.

Declaring an Interface

Interfaces support the same optional (?) and readonly modifiers as object types, and can describe methods.

interface User {
  id: number;
  name: string;
  email?: string;
  greet(): string;
}

const u: User = {
  id: 1,
  name: "Zara",
  greet() {
    return "Hi, I'm " + this.name;
  },
};
console.log(u.greet());
Hi, I'm Zara

Extending Interfaces

An interface can extend one or more others, inheriting their properties. This models "is a" relationships cleanly.

interface Entity {
  id: number;
  createdAt: Date;
}
interface Order extends Entity {
  customerId: number;
  total: number;
}

const order: Order = { id: 101, createdAt: new Date("2026-01-05"), customerId: 1, total: 125 };
console.log(order.id, order.createdAt.getFullYear(), order.total);
101 2026 125

Implementing an Interface in a Class

implements makes the compiler check that a class has everything the interface requires.

interface Shape {
  area(): number;
}
class Circle implements Shape {
  constructor(private r: number) {}
  area() {
    return Math.PI * this.r ** 2;
  }
}
console.log(new Circle(2).area().toFixed(2));
12.57

Interface vs Type

  • Both can describe object shapes and are mostly interchangeable
  • Interfaces can be reopened and merged (declaration merging); type aliases cannot
  • Only type can describe unions, tuples, primitives and mapped types
  • Interfaces extend with extends; types combine with & (intersection)
  • A common convention: interface for object shapes and public APIs, type for everything else

Declaration Merging

Declaring the same interface twice merges the members. Libraries use this so you can add fields, for example to the global Window type.

interface Config { apiUrl: string }
interface Config { timeout: number }

const cfg: Config = { apiUrl: "/api", timeout: 5000 };
console.log(cfg);
{ apiUrl: '/api', timeout: 5000 }

Frequently asked questions

Should I use interface or type in TypeScript?

Either works for object shapes. Many teams use interface for objects and classes, and type for unions and other combinations. Be consistent within a project.

Do interfaces exist at runtime?

No. Interfaces are erased during compilation, so you cannot use instanceof with them. Use type guards to check shapes at runtime.

Can an interface extend a type alias?

Yes, as long as the type alias describes an object type. Interfaces can extend classes too.