Course contentsShow
← TypeScript
Lesson 1163 of 1,79627. Declaration FilesFree lesson

The declare module Syntax for Augmentation

Learn the syntax for reopening a module declaration to add new type information to an existing module.

The declare module Syntax for Augmentation

What you'll learn: How to use declare module to reopen an existing module and add new type information to it.

The Core Idea

Module augmentation lets you add types to an existing module after it's been defined — whether that module is in your own codebase or a third-party library. Think of it like adding extra rooms to a house after it's built: the original structure stays the same, but you're extending it with new features.

The declare module syntax is your tool for this. You reference the module by its exact name, then inside the declaration block, you add only the new types, properties, or methods you want to inject.

Basic Syntax

// Original module (e.g., in a library or another file)
export interface User {
  id: number;
  name: string;
}

// Somewhere else in your codebase
declare module './original-module' {
  interface User {
    email: string; // Adding a new property
  }
}

// Now User has id, name, AND email

The declare module block takes a string literal matching the module path. Inside, you use interface merging or add new exports to extend what's already there.

Why This Matters

This is essential when:

  • A library is missing types you need
  • You're customizing a framework (like Express request objects)
  • You want to extend third-party modules without modifying their source

The syntax looks like you're declaring the module again, but you're actually augmenting it — the original definitions remain, and yours are merged in.

Key Takeaway: Use declare module 'module-name' to reopen a module's declaration scope and add new types through interface merging or additional exports.