Module augmentation and declaration merging
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Merge an interface across declarations.
- Augment a third-party module without editing it.
- Avoid accidental replacement when augmenting.
Overview
#Declaration merging
Two interfaces with the same name in the same scope merge.
interface Box { width: number }
interface Box { height: number }
// Box has width and height
Namespaces and enums merge too; classes and type aliases do not.
Augmenting a module
To add a member to an installed package, import it first, then declare the module.
import "express";
declare module "express" {
interface Request { user?: { id: string } }
}
The import makes this an augmentation. Without it, the block replaces the module types.
Augmenting a global
export {};
declare global {
interface Array<T> { last(): T | undefined }
}
Risk
Augmentation is global. A wrong augmentation leaks into every file and can conflict with another package. Keep augmentations small, in one file, and document why.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What members does `Config` have? ```ts interface Config { host: string } interface Config { port: number } ```
Exercise 2Implement
Augment the `express` Request interface to add an optional `user: { id: string }` without replacing the module.
starter.typescriptimport "express"; declare module "express" { // your code }Reveal the reference answer
reference.typescriptimport "express"; declare module "express" { interface Request { user?: { id: string }; } }Authored expected output: Compile-time: Request now has an optional user. No runtime output.
The interface merges with the existing Request, adding one member.
Exercise 3Debug
This replaces the whole module type instead of augmenting it. Fix it. ```ts declare module "express" { interface Request { user?: { id: string } } } ```
starter.typescriptdeclare module "express" { interface Request { user?: { id: string } } }Reveal the reference answer
reference.typescriptimport "express"; declare module "express" { interface Request { user?: { id: string } } }Authored expected output: Compile error resolved: adding import "express"; turns the block into an augmentation so the original express types survive.
Without an import the file is a script and the declaration replaces the module.
Common misconceptions
#- Augmenting a module redefines it.
- Type aliases merge like interfaces.
- A global augmentation only affects the current file.
Follow-up practice
#- typescript-import-type
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.