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

What Module Augmentation Means

Understand how TypeScript allows you to extend existing module types by declaring additional exports or properties.

What Module Augmentation Means

You'll learn: How TypeScript lets you extend and add to existing module types without modifying the original source.

The Core Idea

Module augmentation is TypeScript's mechanism for extending existing modules with additional type information. Think of it like adding a custom room to a house you don't own — you're enhancing what's already there without rebuilding the original structure.

When you import a third-party library or even one of your own modules, you sometimes discover that it's missing types for properties or methods you know exist, or you want to add new capabilities. Module augmentation lets you "reopen" that module's type definition and declare additional exports, properties, or methods.

Why This Matters

Imagine a library gives you an object with certain typed properties, but you've extended it at runtime with custom fields. Without module augmentation, TypeScript would complain those fields don't exist. With augmentation, you can teach TypeScript about your additions.

You're essentially saying: "This module already exists, but I'm declaring extra stuff that should also be considered part of it."

How It Connects

You've already seen declaration merging with interfaces — where multiple interface declarations with the same name automatically merge together. Module augmentation applies that same principle but at the module level, letting you extend modules imported from elsewhere.

The syntax uses declare module 'module-name' to target a specific module, then adds declarations inside that block. TypeScript merges your augmentations with the original module's types.

Key Takeaway: Module augmentation allows you to extend existing module types by declaring additional exports or properties, giving you type safety for runtime extensions without modifying the original code.