Interfaces and type aliases

Topic 2 of 4 in Objects and functions7 min readIntermediateFree course

Overview

You’ve seen type User = { … }. TypeScript has a second way to name an object shape: the interface. They overlap a lot, which confuses newcomers — so this lesson shows both, what each can do that the other can’t, and a simple way to choose.

Type aliases

A type alias gives a name to any type:

TypeScript
type UserId = string;
type Point = { x: number; y: number };
type Status = "draft" | "published" | "archived";   // a union
type Pair = [string, number];                        // a tuple
type Formatter = (value: number) => string;          // a function type

An alias doesn’t create a new type; it’s a name for an existing one. UserId and string are interchangeable — the name just documents intent.

Interfaces

An interface names an object shape:

TypeScript
interface User {
  name: string;
  email: string;
  nickname?: string;
  readonly id: number;
}

Everything you learned in Object types — optional, readonly, methods, nesting — works the same way. Note there’s no =.

interface vs type alias. interface User { name: string; email: string; } and type User = { name: string; email: string; }; describe the same shape. Both describe object shapes. Extending: interface uses extends, type uses & (intersection). Unions, tuples and primitives: only type. Mapped and conditional types: only type. Declaration merging: an interface can be reopened; a type alias gives an error. For object shapes either works. Pick one style per project; use type when you need unions or type operators.

Extending: extends and &

Interfaces build on each other with extends:

TypeScript
interface Person {
  id: number;
  name: string;
}

interface Member extends Person {
  joined: Date;
}

const m: Member = { id: 1, name: "Asha", joined: new Date() };

An interface can extend several others: interface Admin extends Member, Auditor { … }.

Type aliases get the same result with an intersection, &, which means “has everything from both”:

TypeScript
type Person = { id: number; name: string };
type Member = Person & { joined: Date };

You can mix the two: an interface can extend a type alias (if it’s an object type), and a type alias can intersect interfaces. Intersection types covers & in depth.

One difference: if extends creates a conflict — say, id: number in the parent and id: string in the child — the interface reports an error right away. An intersection quietly produces an impossible property type (never). That makes extends a little safer for building object hierarchies.

Declaration merging

Declare the same interface name twice, and TypeScript merges them:

TypeScript
interface Settings {
  theme: string;
}
interface Settings {
  fontSize: number;
}

const s: Settings = { theme: "dark", fontSize: 14 };   // needs both

A type alias with the same name is an error (Duplicate identifier 'Settings').

Merging is occasionally useful — mostly for adding properties to types you don’t own, such as declaring an extra global property on Window, or adding fields to a library’s request object:

TypeScript
declare global {
  interface Window {
    analytics: { track(event: string): void };
  }
}

In everyday application code it’s more often a surprise than a feature.

What only type aliases can do

TypeScript
type Status = "draft" | "published";              // unions
type Entry = [name: string, score: number];       // tuples
type Callback = (error: Error | null) => void;    // function types (interfaces can, but clumsily)
type Nullable<T> = T | null;                      // aliases of any generic type
type ReadonlyUser = Readonly<User>;               // results of utility, mapped and conditional types

Which should I use?

Both are fine for object shapes, and teams choose differently. Two common conventions:

  1. Interfaces for object shapes, type aliases for everything else. This was the TypeScript handbook’s long-standing advice, and it gives slightly clearer errors when extending.
  2. Type aliases everywhere, for one consistent style — you’ll need type for unions anyway.

This course mostly uses type, and interface where merging or extends makes a point clearer. Whatever you choose, be consistent within a project (a linter rule can enforce it).

Naming conventions

  • Use PascalCase: User, OrderItem, ApiResponse.
  • Don’t prefix interfaces with I (IUser) — that’s a habit from other languages, not TypeScript style.
  • Name types after the thing, not the type system: Product, not ProductType or ProductInterface.

Hovering shows the shape

Hover over a variable typed with an alias, and the editor shows the name (const m: Member). Hover over the name to see its full definition. Use F12 (go to definition) to jump to where a type is declared — in big projects, that’s how you learn the data model.

Rule of thumb: name every shape you use more than once. Use type when you need unions, tuples or type operators; use interface or type for plain objects — consistently.

Exercise

Model a library

Model a small library system. (1) A type alias Id that is a string or a number. (2) An interface Person with id: Id and name: string. (3) An interface Book with isbn, title and author (all strings) and year (number). (4) An interface Member that extends Person and adds joined: Date and borrowed: Book[]. (5) A type alias Librarian built from Person with an intersection, adding desk: number. Write summary(member: Member): string returning e.g. Asha (#7) has 2 books: Dune, Emma, and canBorrow(member: Member, limit = 3): boolean. Create a member and a librarian and print the results.

TypeScript
// 1–5. Types

// summary() and canBorrow()

// Try them out

Summary

  • A type alias (type Name = …) names any type — objects, unions, tuples, functions.
  • An interface (interface Name { … }) names an object shape and can extend other interfaces.
  • Intersections (A & B) combine types the way extends combines interfaces.
  • Interfaces with the same name merge; type aliases can’t be redeclared.
  • For object shapes either works — pick one style for a project, and use type for everything an interface can’t express.