All academies
Roadmap/Intermediate

Level 7

Type Aliases

Name any type with the type keyword: objects, unions, tuples, functions, and intersections. Understand how aliases differ from interfaces and when to choose each.

18 min read + quiz

What a Type Alias Is

A type alias binds a name to any type: 'type UserID = number' makes UserID mean number wherever it appears. The word 'alias' is precise: you are not creating a new, distinct type, only a new name for an existing one. A UserID and a plain number are fully interchangeable; the alias improves readability without changing checking.

Aliases pay off immediately for complex types. A signature like '(handler: (event: string, payload: unknown) => void) => void' is hard to read inline; naming the inner function type EventHandler turns it into '(handler: EventHandler) => void'. Names also centralize change: update the alias definition once and every usage follows.

Like interfaces, aliases are compile-time constructs, fully erased from the emitted JavaScript. They cost nothing at runtime.

type UserID = number;
type EventHandler = (event: string, payload: unknown) => void;

function fetchUser(id: UserID): string {
  return "user-" + id;
}

const onEvent: EventHandler = (event, payload) => {
  console.log("Received " + event, payload);
};

fetchUser(42);            // a plain number is a valid UserID
onEvent("login", { at: Date.now() });

Aliases give readable names to primitives and function types alike.

Aliases Can Name Anything - Interfaces Cannot

Here is the key difference from interfaces: an interface can only describe an object shape (or function/class shape), while a type alias can name literally any type. Unions, primitives, tuples, literal types, and combinations of all of these are alias-only territory.

The most common alias in day-to-day TypeScript is the literal union: 'type Status = "idle" | "loading" | "success" | "error"'. There is no interface equivalent, since a union is not an object shape. The same goes for tuples like 'type Pair = [number, number]' and mixed unions like 'type Input = string | string[]'.

This is why 'interface vs type' is not a rivalry: for anything that is not an object shape, aliases are simply the only option. The real choice only arises for object shapes, where both work.

// None of these can be expressed as an interface:
type Status = "idle" | "loading" | "success" | "error";
type Pair = [number, number];
type Input = string | string[];

function normalize(input: Input): string[] {
  if (typeof input === "string") {
    return [input];
  }
  return input;
}

const point: Pair = [3, 4];
const state: Status = "loading";
console.log(normalize("solo"), normalize(["a", "b"]), point, state);

Unions, tuples, and literal types are exclusive to type aliases.

Object Aliases and Intersections

Type aliases describe object shapes just as well as interfaces, with identical syntax inside the braces: optional properties, readonly, methods, and index signatures all work. For a plain object shape, 'type User = { ... }' and 'interface User { ... }' are interchangeable in almost every situation.

Where interfaces use extends, aliases combine with the intersection operator (&). 'type Post = Identified & Timestamped & { title: string }' produces a type with all members of all three parts. Intersections are more flexible, since they can combine aliases, interfaces, and inline shapes in one expression, but extends gives better error messages when parents conflict, because the compiler checks compatibility at declaration time.

One caution with intersections: combining incompatible members does not error at the definition; it produces a property of type never that only fails when you try to create a value. 'extends' would have flagged the conflict immediately. This is one concrete reason to prefer interfaces for large hierarchies of object shapes.

type Identified = { readonly id: number };
type Timestamped = { createdAt: string };

type Post = Identified & Timestamped & {
  title: string;
};

const post: Post = {
  id: 1,
  createdAt: "2026-07-11",
  title: "Intersections in action",
};

console.log(post.id, post.title);

The & operator merges shapes - the alias world's equivalent of extends.

Aliases vs Interfaces: Making the Call

The differences that matter in practice: aliases can name any type while interfaces are object-shapes only; interfaces support declaration merging while duplicate alias names are a hard error; and interfaces can be extended and implemented in ways that produce slightly clearer error messages for deep hierarchies.

The duplicate-name behavior cuts both ways. Merging makes interfaces the right tool for augmenting library types, but it also means a copy-pasted interface name merges silently. An alias in the same situation errors immediately, which is usually what you want inside application code.

Sensible default: use interfaces for object shapes that form your public API or that classes implement, and aliases for everything else, especially unions, function types, and tuples. Many respected teams simplify further and use aliases for everything except when merging is required. Consistency within a codebase matters more than the choice itself.

// Alias: duplicate names are an immediate, helpful error.
type Config = { theme: string };
// type Config = { fontSize: number };
// Error: Duplicate identifier 'Config'.

// Both can describe the same object shape:
interface AsInterface { x: number }
type AsAlias = { x: number };

const a: AsInterface = { x: 1 };
const b: AsAlias = a;       // structurally identical, interchangeable
console.log(a.x + b.x);

Aliases refuse duplicates; for plain object shapes the two forms are interchangeable.

Building Vocabulary with Aliases

Aliases really shine when you use them to build a domain vocabulary. Instead of scattering '"pending" | "shipped" | "delivered"' through a codebase, define 'type OrderStatus' once and let every function speak the same language. When a new status is added, one edit updates the contract everywhere, and the compiler lists every switch or if-chain that needs a new case.

Aliases can reference other aliases, so you can compose small named pieces into larger ones: an Order type built from OrderStatus, LineItem[], and Address reads almost like documentation. Aim for names that describe meaning (Currency, EmailAddress) rather than structure (StringNumberPair).

A preview of what is ahead: aliases can also take generic parameters, like 'type ApiResponse<T> = { data: T; error: string | null }'. You will study generics properly in a later lesson, but be aware that most of the powerful utility types in TypeScript's standard library, like Partial and Pick, are just generic type aliases.

type OrderStatus = "pending" | "shipped" | "delivered";
type LineItem = { sku: string; quantity: number; unitPrice: number };

type Order = {
  id: string;
  status: OrderStatus;
  items: LineItem[];
};

function orderTotal(order: Order): number {
  return order.items.reduce(
    (sum, item) => sum + item.quantity * item.unitPrice,
    0
  );
}

const order: Order = {
  id: "ord-1",
  status: "pending",
  items: [{ sku: "TS-101", quantity: 2, unitPrice: 30 }],
};

console.log(order.status, orderTotal(order)); // pending 60

Small named types compose into a readable domain model.

Try it yourself

This lesson's example is preloaded in the playground — break it, fix it, extend it.

Open in playground

Check your understanding

Question 1 · Multiple choice

Which of these types can ONLY be expressed with a type alias, not an interface?

Question 2 · True or false

Because 'type UserID = number' creates a distinct type, passing a plain number where a UserID is expected is a compile error.

Question 3 · Predict the output

What does this program print?

type Input = string | string[];

function count(input: Input): number {
  if (typeof input === "string") {
    return 1;
  }
  return input.length;
}

console.log(count("solo"));
console.log(count(["a", "b", "c"]));

Question 4 · Find the error

This code fails to compile. Why?

type Theme = { background: string };
type Theme = { foreground: string };

const theme: Theme = {
  background: "black",
  foreground: "white",
};
console.log(theme);

Question 5 · Multiple choice

Given 'type A = { x: number }' and 'type B = { y: number }', how do you create a type with both x and y?