TExcelTAD
Home › TypeScript › TS Type Aliases & Interfaces

TypeScript Type Aliases & Interfaces

Writing { title: string; pages: number } every time you need a book type gets tedious fast. TypeScript gives you two ways to name a type so you can reuse it: type aliases and interfaces. For object shapes they are almost interchangeable; this lesson shows both and explains the differences.

Type aliases

A type alias gives a name to any type with the type keyword:

ts · try it yourself
type UserId = number;
type Point = { x: number; y: number };
type Status = "active" | "suspended";

const id: UserId = 42;
const start: Point = { x: 0, y: 10 };
let state: Status = "active";

function distance(a: Point, b: Point): number {
  return Math.hypot(b.x - a.x, b.y - a.y);
}

console.log(id, state, distance(start, { x: 3, y: 14 }));
42 active 5

An alias does not create a new type — UserId is just another name for number. Aliases can name primitives, unions, tuples, functions, and anything else you can write as a type.

Interfaces

An interface describes the shape of an object using the interface keyword:

ts · try it yourself
interface Book {
  title: string;
  author: string;
  pages: number;
  isbn?: string;
}

function summary(book: Book): string {
  return `${book.title} by ${book.author} (${book.pages} p.)`;
}

const favourite: Book = { title: "Norwegian Wood", author: "Haruki Murakami", pages: 296 };
console.log(summary(favourite));
Norwegian Wood by Haruki Murakami (296 p.)

Using a named type also makes error messages much shorter and clearer:

ts · try it yourself
interface Book {
  title: string;
  pages: number;
}

const draft: Book = { title: "Untitled" };
main.ts(6,7): error TS2741: Property 'pages' is missing in type '{ title: string; }' but required in type 'Book'.

Extending interfaces

An interface can extend one or more others, inheriting their properties:

ts · try it yourself
interface Animal {
  species: string;
  legs: number;
}

interface Pet extends Animal {
  petName: string;
  vaccinated: boolean;
}

const dog: Pet = { species: "dog", legs: 4, petName: "Mít", vaccinated: true };
console.log(`${dog.petName} is a ${dog.legs}-legged ${dog.species}`);
Mít is a 4-legged dog

Type aliases achieve the same with an intersection (&), which combines several types into one:

ts · try it yourself
type Animal = { species: string; legs: number };
type Pet = Animal & { petName: string };

const cat: Pet = { species: "cat", legs: 4, petName: "Mun" };
console.log(cat.petName, cat.species);
Mun cat

Declaration merging

Declaring an interface twice with the same name merges the two declarations. This is how libraries let you add properties to existing types (for example, extending Window or Express's Request). Type aliases can't do this — a duplicate name is an error:

ts · try it yourself
interface Settings {
  theme: string;
}
interface Settings {
  fontSize: number;
}

const s: Settings = { theme: "dark", fontSize: 14 };
console.log(s);

type Config = { debug: boolean };
type Config = { verbose: boolean };
main.ts(11,6): error TS2300: Duplicate identifier 'Config'.
main.ts(12,6): error TS2300: Duplicate identifier 'Config'.

Describing functions

Both forms can describe functions. The alias syntax is usually more readable:

ts
type Formatter = (value: number) => string;

interface Comparator {
  (a: string, b: string): number;
}

const asPrice: Formatter = (v) => `${v.toFixed(2)}`;
const byLength: Comparator = (a, b) => a.length - b.length;

type vs interface

Feature type interface
Object shapes yes yes
Unions, tuples, primitives yes no
Extend / combine & intersection extends
Declaration merging no yes
Implemented by a class yes (object types) yes
Error messages shows the alias name shows the interface name

Tip: A common convention: use interface for object shapes (especially public APIs that others may extend) and type for unions, tuples, function types and other compositions. What matters most is being consistent within a project.

Test yourself

Which can a type alias describe that an interface cannot?

  1. An object with methods
  2. A union like "a" | "b"
  3. Optional properties
  4. Readonly properties

What happens when you declare interface Box twice with different properties?

  1. Compile error: duplicate identifier
  2. The second replaces the first
  3. The declarations are merged
  4. Only the first is used

How do you combine two type aliases A and B into one type with all properties of both?

  1. A | B
  2. A & B
  3. A extends B
  4. A + B