Module augmentation lets you add new features or fix things in existing code without changing the original files. It helps keep your code organized and safe.
You want to add new functions or types to a library you use but can't edit its code.
You need to fix or improve type definitions from a third-party package.
You want to add extra properties to existing objects or modules in your project.
You want to customize behavior of a module without copying or rewriting it.
Syntax
Typescript
declare module'module-name' {
// add new types, interfaces, or values here
}
Use declare module with the exact module name you want to extend.
Put your additions inside the curly braces to safely add to the module.
Examples
This adds a new optional userId property to the Request interface from the Express library.
This example shows how to add a new property userId to Express's Request object using module augmentation. Then it uses that property in middleware and a route.
Typescript
import express from'express';
// Module augmentation to add userId to Request
declare module'express' {
interface Request {
userId?: string;
}
}
const app = express();
app.use((req, res, next) => {
req.userId = 'abc123';
next();
});
app.get('/', (req, res) => {
res.send(`User ID is ${req.userId}`);
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
OutputSuccess
Important Notes
Module augmentation only changes types or declarations, it does not change runtime code by itself.
Always use the exact module name as in the original import to avoid errors.
Summary
Module augmentation helps you safely add or fix types in existing modules.
It is useful when you can't or don't want to change original code.
Use declare module 'name' with additions inside curly braces.
Practice
(1/5)
1. Why do we need module augmentation in TypeScript?
easy
A. To add or fix types in existing modules without changing original code
B. To create new modules from scratch
C. To delete unused modules from a project
D. To convert JavaScript files into TypeScript files automatically
Solution
Step 1: Understand the purpose of module augmentation
Module augmentation allows adding or fixing types in existing modules safely without modifying their original source code.
Step 2: Compare with other options
Creating new modules, deleting modules, or converting files are unrelated to module augmentation's purpose.
Final Answer:
To add or fix types in existing modules without changing original code -> Option A
Quick Check:
Module augmentation = add/fix types safely [OK]
Hint: Module augmentation = safely add or fix types [OK]
Common Mistakes:
Thinking it creates new modules
Confusing with file conversion
Assuming it deletes modules
2. Which syntax correctly starts a module augmentation in TypeScript?
easy
A. augment module 'module-name' { }
B. declare module 'module-name' { }
C. module augment 'module-name' { }
D. extend module 'module-name' { }
Solution
Step 1: Recall the correct syntax for module augmentation
The correct syntax uses the keyword declare module 'module-name' { } to add types.
Step 2: Identify incorrect keywords
Keywords like augment or extend are not valid TypeScript syntax for module augmentation.
Final Answer:
declare module 'module-name' { } -> Option B
Quick Check:
Syntax starts with declare module [OK]
Hint: Module augmentation starts with declare module [OK]
Common Mistakes:
Using 'augment' or 'extend' keywords
Omitting the declare keyword
Using incorrect quotes or braces
3. What will be the output of this TypeScript code?
D. Config interface is not imported or globally available
Solution
Step 1: Check scope of augmented interface
The interface Config is declared inside module 'my-lib', so it is not globally visible.
Step 2: Analyze usage of Config variable
The variable config uses Config without importing or referencing the module, causing a type error.
Final Answer:
Config interface is not imported or globally available -> Option D
Quick Check:
Augmented types need proper import or module reference [OK]
Hint: Augmented types need import or module prefix [OK]
Common Mistakes:
Assuming interface is global automatically
Ignoring module boundaries
Confusing syntax errors with scope errors
5. You want to add a new method logInfo() to an existing interface Logger from a third-party module without modifying its source. How should you do it?
hard
A. Modify the third-party module source code directly to add logInfo()
B. Create a new interface named Logger with logInfo() and use it instead
C. Use module augmentation with declare module 'module-name' and add logInfo() inside Logger interface
D. Extend Logger interface in your code without module augmentation
Solution
Step 1: Understand the goal
You want to add a method to an existing interface from a third-party module without changing its source code.
Step 2: Apply module augmentation
Module augmentation with declare module 'module-name' lets you safely add logInfo() to the Logger interface inside the module.
Final Answer:
Use module augmentation with declare module and add logInfo() inside Logger interface -> Option C
Quick Check:
Module augmentation = add methods safely [OK]
Hint: Add methods via declare module, not by editing source [OK]
Common Mistakes:
Editing third-party source directly
Creating new interface instead of augmenting
Extending interface without module context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.