Interfaces and type aliases
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:
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 typeAn 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:
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 =.
Extending: extends and &
Interfaces build on each other with extends:
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”:
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:
interface Settings {
theme: string;
}
interface Settings {
fontSize: number;
}
const s: Settings = { theme: "dark", fontSize: 14 }; // needs bothA 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:
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
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 typesWhich should I use?
Both are fine for object shapes, and teams choose differently. Two common conventions:
- 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.
- Type aliases everywhere, for one consistent style — you’ll need
typefor 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, notProductTypeorProductInterface.
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
typewhen you need unions, tuples or type operators; useinterfaceortypefor plain objects — consistently.
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.
// 1–5. Types
// summary() and canBorrow()
// Try them out// 1–5. Types
type Id = string | number;
interface Person {
id: Id;
name: string;
}
interface Book {
isbn: string;
title: string;
author: string;
year: number;
}
interface Member extends Person {
joined: Date;
borrowed: Book[];
}
type Librarian = Person & { desk: number };
// summary() and canBorrow()
function summary(member: Member): string {
const titles = member.borrowed.map((book) => book.title).join(", ");
const count = member.borrowed.length;
return `${member.name} (#${member.id}) has ${count} book${count === 1 ? "" : "s"}: ${titles}`;
}
function canBorrow(member: Member, limit = 3): boolean {
return member.borrowed.length < limit;
}
// Try them out
const dune: Book = { isbn: "978-0441013593", title: "Dune", author: "Frank Herbert", year: 1965 };
const emma: Book = { isbn: "978-0141439587", title: "Emma", author: "Jane Austen", year: 1815 };
const asha: Member = { id: 7, name: "Asha", joined: new Date("2026-01-15"), borrowed: [dune, emma] };
const ravi: Librarian = { id: "L-2", name: "Ravi", desk: 4 };
console.log(summary(asha)); // Asha (#7) has 2 books: Dune, Emma
console.log(canBorrow(asha)); // true
console.log(canBorrow(asha, 2)); // false
console.log(`${ravi.name} works at desk ${ravi.desk}`);Summary
- A type alias (
type Name = …) names any type — objects, unions, tuples, functions. - An interface (
interface Name { … }) names an object shape and canextendother interfaces. - Intersections (
A & B) combine types the wayextendscombines 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
typefor everything an interface can’t express.