Sub topic 12
Extending Existing Modules with Augmentation
This chapter assumes that you know TypeScript interfaces, import and export statements, and how tsc resolves files from a project. Module augmentation lets you add type information to a module you do not own, such as a library or another file in your project.
What module augmentation does
A module augmentation reopens an existing module and merges new declarations into it. This is useful when a library supports plugins that add properties or methods, but its published types do not know about your plugin.
The added declarations affect type checking only. They do not create a runtime property or method. Your plugin must still add the actual behavior when the program runs.
For example, suppose user.ts exports a User interface. A separate file can add an optional nickname property:
// user.ts
export interface User {
id: number;
name: string;
}
// user-extras.ts
import "./user.js";
declare module "./user.js" {
interface User {
nickname?: string;
}
}
// app.ts
import type { User } from "./user.js";
import "./user-extras.js";
const user: User = {
id: 1,
name: "Ada",
nickname: "ace"
};
console.log(user.nickname);The import "./user.js" statement in user-extras.ts makes that file an external module. The declare module block then augments the existing module rather than declaring an unrelated ambient module.
The augmentation must use the same module specifier that consumers use. In a Node.js ESM project, that often means writing ./user.js in TypeScript source even though the source file is named user.ts.
Augmenting a library
Library documentation may provide a module name for augmentation. For example:
// express-user.d.ts
import "express-serve-static-core";
declare module "express-serve-static-core" {
interface Request {
userId?: string;
}
}After this declaration is included in the project, request handlers can read request.userId without a type error. The application still needs authentication middleware that assigns the value at runtime.
Keep augmentation files inside the compiler's include paths, or list them through the project's configuration. If TypeScript never reads the file, the augmentation has no effect.
Limits and common errors
Module augmentation can merge into existing named declarations, such as interfaces and classes. It cannot add a new top-level export to the target module. It also cannot augment a default export directly because a default export has no usable name for declaration merging.
Common errors include:
- Using the source filename, such as
./user.ts, when consumers import./user.js. - Forgetting the side-effect import that makes the augmentation file a module.
- Placing the declaration file outside
includeorfiles. - Assuming an optional augmented property exists at runtime.
- Replacing the module's declarations accidentally by writing an ambient declaration without importing the original module.
Module augmentation versus global augmentation
A module augmentation changes one named module. A global augmentation changes a global type such as Window or Array, and it must appear inside an external module:
export {};
declare global {
interface Window {
analyticsEnabled?: boolean;
}
}Use global augmentation sparingly. Global names affect every file in the program and can create conflicts between packages.
Exercise
Why does an augmentation file usually contain import "./user.js"; before its declare module block?
Show answer
The import makes the file an external module and ensures that the declaration merges with the existing module instead of declaring a replacement ambient module.