TypeScriptTypeScript10 min read

TypeScript Interfaces vs Types: When to Use Each

Understand the difference between interfaces and types in TypeScript. Learn when to use interfaces, when to use types, and why it matters. This knowledge helps you write better TypeScript code.

David Lee
December 18, 2025
0.0k0

One of the most common questions in TypeScript: should I use interface or type? The answer isn't always obvious, but understanding the differences helps you make the right choice.

Interfaces Explained

Interfaces define the shape of an object. They're great for describing contracts - what properties an object should have. They can be extended and merged, which makes them flexible for building up complex types.

Types Explained

Types are more flexible - they can represent objects, unions, intersections, primitives, and more. They're more powerful but sometimes less intuitive. Types can't be merged like interfaces.

When to Use Interfaces

Use interfaces for object shapes, especially when you might extend or merge them. They're also better for declaration merging. If you're defining the structure of an object, interfaces are usually the way to go.

When to Use Types

Use types for unions, intersections, mapped types, or when you need features interfaces don't support. Types are more powerful for complex type manipulations.

Best Practices

I'll show you real examples of when to use each, and common patterns. Understanding this helps you write cleaner, more maintainable TypeScript code.

#TypeScript#Interfaces#Types#Type System

Common Questions & Answers

Q1

What's the difference between interface and type?

A

Interfaces can be extended and merged (declaration merging), are better for object shapes, and can be implemented by classes. Types are more flexible (unions, intersections, mapped types), can't be merged, and can represent more than just objects. Use interfaces for object shapes, types for complex type operations.

typescript
// Interface - can be extended and merged
interface User {
  name: string;
  age: number;
}

interface User {
  email: string;  // Merges with previous declaration
}

interface Admin extends User {
  role: 'admin';
}

// Type - more flexible
type Status = 'active' | 'inactive' | 'pending';  // Union

type UserWithStatus = User & { status: Status };  // Intersection

// Types can't be merged
// type User { email: string; }  // Error - can't merge types
Q2

When should I use interface vs type?

A

Use interfaces for object shapes, especially when you might extend them or need declaration merging. Use types for unions, intersections, mapped types, tuples, or when you need features interfaces don't support. For simple object shapes, either works, but interfaces are more conventional.

typescript
// Interface - good for object shapes
interface Config {
  apiUrl: string;
  timeout: number;
}

// Type - good for unions and complex types
type ID = string | number;
type EventCallback = (event: Event) => void;

// Type - good for mapped types
type Optional<T> = {
  [K in keyof T]?: T[K];
};

// Type - good for tuples
type Point = [number, number];

// Interface - can be implemented
class MyClass implements Config {
  apiUrl = 'https://api.example.com';
  timeout = 5000;
}