Course contentsShow
← TypeScript
Lesson 364 of 1,7968. Interfaces vs Type AliasesFree lesson

Declaration Merging with Modules

Using declaration merging to augment module exports and extend existing interfaces.

Declaration Merging with Modules

What you'll learn: How to use declaration merging to extend module exports and augment existing interfaces from third-party packages.

What Is Declaration Merging with Modules?

You already know that interfaces can merge when declared multiple times in the same scope. TypeScript extends this power to modules — you can reopen a module namespace and add or extend types, even for code you imported from elsewhere.

This is incredibly useful when you need to augment types from libraries you don't control, or when you want to extend built-in global types.

How It Works

When you import a module, TypeScript creates a namespace for it. You can declare additional properties or extend interfaces within that module's namespace using declare module 'module-name'.

The syntax looks like this:

// Suppose a library exports this interface
// (in node_modules/some-lib/index.d.ts)
export interface User {
  name: string;
}

// In your code, you can augment it
declare module 'some-lib' {
  interface User {
    email: string; // Adding a new property
  }
}

// Now User has both name AND email

The interface User merges across the module boundary, combining properties from both declarations.

Why This Matters

This technique lets you:

  • Add type definitions to untyped or partially-typed libraries
  • Extend framework types with custom properties
  • Patch missing or incorrect type definitions without forking packages

Key Takeaway: Declaration merging with modules allows you to augment or extend interfaces from imported modules, giving you control over third-party types without modifying their source code.