Type Aliases

Give any type, not just an object shape, a reusable name.

What is it?

Not every type you want to reuse is an object. It might be a set of allowed strings like "pending" | "shipped" | "delivered", a function signature, or just a long type you don't want to repeat.

A type alias, written with the type keyword, gives any type a name: an object shape, a union of values, a tuple, a function signature. Then you use the name everywhere instead of the full definition.

Explain like I'm 10

A type alias is a nickname for a phrase you keep repeating. Instead of saying "the delivery status, which is pending, shipped or delivered" every time, you just say "OrderStatus" and everyone knows what you mean.

Examples

Naming a union of specific values

type OrderStatus = "pending" | "shipped" | "delivered";

let status: OrderStatus = "shipped";

status = "cancelled";
// Error: Type '"cancelled"' is not assignable to type 'OrderStatus'.

OrderStatus isn't an object shape; it's a fixed set of allowed strings. Type aliases can name this kind of type. Interfaces can't.

A type alias for an object shape (like an interface)

type Point = {
  x: number;
  y: number;
};

function distanceFromOrigin(p: Point): number {
  return Math.sqrt(p.x ** 2 + p.y ** 2);
}

// interface vs type, for an object shape, are nearly interchangeable:
interface PointInterface {
  x: number;
  y: number;
}

For plain object shapes, type and interface do almost the same job. The differences show up in advanced cases: interfaces can be reopened to add properties (declaration merging), and type aliases can describe unions, tuples and other non-object types.

How it works

Like an interface, a type alias exists only at compile time. The compiler substitutes it wherever it's used, and it leaves no trace in the compiled JavaScript.

The difference is what it can name. An interface only describes object shapes. A type alias can name any type: unions, tuples, function signatures, primitives and object shapes.

Why does it exist?

Object shapes aren't the only types worth reusing. Sets of allowed strings, function signatures and combinations of types all benefit from one readable name. Type aliases cover everything an interface can't express.

When to use it

Use a type alias to name a union (like a fixed set of strings), a tuple, a function signature, or anything that isn't strictly "an object with these properties." It's also fine for object shapes if your team prefers type over interface for consistency.

When not to use it

If an object shape may need more properties merged in from elsewhere later (common in libraries), use an interface: it supports declaration merging and a type alias doesn't. For everyday object shapes, either works.

Common mistakes

  • Thinking type and interface are fully interchangeable. Type aliases can express unions and other non-object types that interfaces can't.

  • Trying to "reopen" a type alias to add properties, as you can with an interface. Type aliases can't be merged.

  • Giving a type alias the same name as an existing variable or type in the same scope, causing a naming collision.

Practice exercises

  1. Easy:

    Create a type alias Direction that can only be "up", "down", "left" or "right", and declare a variable of that type.

  2. Medium:

    Create a type alias Coordinate for a [number, number] tuple. Write a function move(point: Coordinate, direction: Direction): Coordinate that returns a new coordinate.

  3. Hard:

    Write a type alias Comparator for a function that takes two numbers and returns a number. Then write sortNumbers(nums: number[], compare: Comparator): number[] that uses it.

Interview questions

What can a type alias describe that an interface cannot?

Unions, tuples, function signatures and primitive types. A type alias can name any type; an interface only describes object shapes.

What is declaration merging, and which of `type` or `interface` supports it?

Declaring the same named shape more than once and having TypeScript combine them into one. Only interface supports it; declaring a type alias name twice is an error.

For a simple object shape, does it matter whether you use `type` or `interface`?

Usually not. Both check objects the same way. It comes down to team convention, unless you need a feature only one supports.

Does writing `type ID = string;` create a genuinely new type distinct from `string`?

No. It's another name for the same type. An ID works anywhere a string does and vice versa, unlike "branded" or "nominal" types in some languages.

Given `type ID = string | number; let id: ID = "abc123"; id = 42;`, does the second assignment compile?

Yes. 42 is a number, which the union allows. id = true; would fail, since boolean isn't in the union.

Can a type alias reference itself, like `type Tree = { value: number; children: Tree[] };`?

Yes, for object shapes. It's a recursive type alias. TypeScript resolves it lazily instead of expanding it forever, which lets you model nested structures like trees or JSON with one named type.

What happens if you declare `type Status = "active" | "inactive";` twice in the same scope?

A compile error. Unlike interface, a type alias can't be declared twice; there's no merging.

How would you type a variable that holds a function signature, like a comparator used for sorting?

With a type alias for the function shape: type Comparator = (a: number, b: number) => number;. Any function assigned to a variable of that type is checked against those parameters and return type.

Given `type Status = "active" | "inactive"; function setStatus(s: Status) {}`, why does `setStatus("Active")` fail to compile?

Status allows only the exact strings "active" and "inactive". String literal types are case-sensitive, so "Active" isn't allowed.

How would you express "a `User` extended with an extra `permissions` field" using type aliases instead of `interface extends`?

With an intersection: type Admin = User & { permissions: string[] };. The & combines both shapes, requiring everything from User plus the new field. It's the type-alias equivalent of extends.

You need to model a value that's always either a `User` object or a `Guest` object, distinguished by a shared `kind` field. Would you reach for `interface` or `type` here, and why?

A type alias, because the value is a union (User | Guest). An interface can only describe a single object shape. Each branch can still be an interface; only the union needs type.

Why can a type alias name "literally any type," while an interface is limited to object shapes?

A type alias is a name bound to any type expression: a union, tuple, function signature, primitive or object shape. Interface syntax only declares named properties, so it can't represent something like a union of strings.

If you write `type Point = { x: number; y: number };` and separately `interface PointInterface { x: number; y: number }`, are values typed as one assignable to the other?

Yes. TypeScript checks structurally and both describe the same shape, so each is assignable to the other.

What's the main practical trade-off a team gives up by adopting "always use `type`, never `interface`" as a blanket style rule?

Declaration merging: reopening a declared shape to add properties later, which some patterns (especially library type definitions) rely on. In most application code it rarely matters.

Can a tuple type be given a name with a type alias, and why would you want that?

Yes, for example type Coordinate = [number, number];. Every function can then use Coordinate instead of repeating [number, number], and there's one definition to change.

Why can't an interface be used to name a type like `type Direction = "up" | "down" | "left" | "right";`?

Interface syntax only declares named properties on an object. There's no way to say "one of these four strings" with it, because an interface describes an object's shape, not a value.

Naming a type alias the same as an existing variable in the same scope — is that allowed?

Technically yes: types and values live in separate namespaces, so type User = {...} and let User = ... can coexist. It's confusing, so avoid it. Two types with the same name are still an error.