Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to TypeScript
TypeScript Interfaces vs Types

TypeScript Interfaces vs Types

TypeScript2,003 viewsBy Admin
typescriptinterfacestypes

Advertisement

The Short Answer

Both interface and type describe the shape of data. For object shapes they are almost interchangeable. Use interface for objects/classes, and type for unions, primitives, and tuples.

Defining an Object

interface User {
  id: number;
  name: string;
}

type UserT = {
  id: number;
  name: string;
};

What Only "type" Can Do

// Union types
type Status = "active" | "inactive" | "banned";

// Primitive alias
type ID = string | number;

// Tuple
type Point = [number, number];

What Interfaces Do Better

// Declaration merging — two interfaces with the same name combine
interface Window { customProp: string; }
interface Window { anotherProp: number; }
// Both props now exist on Window

// Extending
interface Admin extends User {
  role: string;
}

Comparison Table

Featureinterfacetype
Object shapes
Unions
Tuples
Declaration merging
extends / implementsvia &

FAQs

Which should I default to?

A common convention: interface for public API object shapes, type for everything else. Consistency matters more than the choice.

Can they reference each other?

Yes — an interface can extend a type and vice versa. More in our TypeScript section.

Advertisement