Skip to content

TypeScript

Interfaces in TypeScript: A Comprehensive Guide

An interface describes the shape of an object. It says "anything that claims to be this type must have these properties." It doesn't contain any implement...

23 Apr 2024 · 2 min read

Interfaces in TypeScript: A Comprehensive Guide

An interface describes the shape of an object. It says "anything that claims to be this type must have these properties." It doesn't contain any implementation — just a contract.

Typescript
interface Person {
  name: string;
  age: number;
}

Any object matching this shape satisfies the interface. No implements keyword required for plain objects:

Typescript
const person: Person = {
  name: 'John',
  age: 30
};

If you try to assign an object missing age, TypeScript stops you at compile time. That's the whole point.

Interfaces with classes

When you use a class, implements makes the contract explicit:

Typescript
class Employee implements Person {
  constructor(public name: string, public age: number) {}

  sayHello() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

The class must satisfy every property and method the interface defines. If it doesn't, the compiler tells you immediately.

Why interfaces matter

  • Catch shape mismatches early. Pass the wrong object to a function and the compiler flags it before runtime.
  • Enable refactoring. Rename a property in the interface and TypeScript shows you every place that needs updating.
  • Document contracts. Reading an interface tells you exactly what a function expects without reading its implementation.

Interface vs Type

In TypeScript, type and interface overlap significantly. Both can describe object shapes. The differences:

  • Interfaces support declaration merging (defining the same interface twice combines them). Types don't.
  • Types support unions and intersections more naturally (type Result = Success | Error).

I use interfaces for object shapes and public API contracts. I use types for unions, intersections, and computed types. But either works in most situations.

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