Skip to content

Types vs Interfaces

type aliases and interfaces can both describe object shapes, and for simple cases they’re nearly interchangeable β€” but they have real differences. interfaces support declaration merging (redeclaring the same interface adds to it) and are generally preferred for public object/class APIs; type aliases can represent things interfaces can’t, like unions, tuples, and mapped types, and are generally preferred for those cases.

// Both can describe an object shape
interface UserInterface { name: string; age: number; }
type UserType = { name: string; age: number };
// Interfaces merge automatically when declared twice (useful for extending libraries)
interface Window { myGlobal: string; }
interface Window { anotherGlobal: number; } // merges with the one above
// type aliases can do things interfaces can't:
type Status = "loading" | "success" | "error"; // union
type Coordinates = [number, number]; // tuple
type Nullable<T> = T | null; // generic alias
type Readonly<T> = { readonly [K in keyof T]: T[K] }; // mapped type
// Extending: interfaces use `extends`, types use `&`
interface Employee extends UserInterface { id: string; }
type EmployeeType = UserType & { id: string };

Trying to declare a union or tuple type as an interface β€” interfaces can only describe object (and function-call) shapes, not unions of unrelated types.

// Error: an interface cannot represent a union
interface Status = "loading" | "success" | "error"; // not valid syntax at all
// Correct -- use a type alias for unions
type Status = "loading" | "success" | "error";
  1. What can a type alias represent that an interface cannot?

    AnswerUnions, tuples, primitive aliases, and mapped/conditional types β€” interfaces are limited to describing object and function-call shapes.
  2. What happens if you declare the same interface name twice in the same scope?

    AnswerTypeScript merges the two declarations into one combined interface (declaration merging) β€” declaring the same type alias twice, by contrast, is a compile error.
  3. How do you extend an existing shape with each β€” interface versus type?

    AnswerInterfaces use interface B extends A { ... }; type aliases use an intersection: type B = A & { ... }.