Sometimes you want to add new features or fix types in libraries you did not write. Augmenting lets you safely add or change types without changing the original code.
Augmenting third-party libraries in Typescript
declare module 'library-name' { // add or change types here interface SomeInterface { newProperty: string; } function newFunction(param: number): void; }
Use declare module 'library-name' to tell TypeScript you are adding types to that library.
Put your new or changed types inside the block. This merges with existing types.
myCustomMethod to lodash's main interface.declare module 'lodash' { interface LoDashStatic { myCustomMethod(): string; } }
user property to Express's Request object to store user info.declare module 'express-serve-static-core' { interface Request { user?: { id: string }; } }
This program adds a new method shout to lodash that makes text uppercase and adds an exclamation mark. Then it calls it and prints the result.
import _ from 'lodash'; declare module 'lodash' { interface LoDashStatic { shout(text: string): string; } } _.shout = function(text: string) { return text.toUpperCase() + '!'; }; console.log(_.shout('hello'));
Augmentations only affect TypeScript's type system, not the actual JavaScript code. You must add the implementation yourself.
Put augmentations in a .d.ts file or at the top of your TypeScript file before usage.
Be careful to match the module name exactly as the library exports it.
Augmenting lets you add or fix types in third-party libraries without changing their code.
Use declare module 'library-name' and add your new types inside.
Remember to add the actual code for new features yourself.
Practice
Solution
Step 1: Understand the goal of augmentation
Augmentation is used to add or fix types in existing third-party libraries without modifying their source code.Step 2: Compare options with this goal
Only To add or fix types without changing the original library code matches this purpose. Other options describe different tasks unrelated to augmentation.Final Answer:
To add or fix types without changing the original library code -> Option AQuick Check:
Augmenting = add/fix types without code change [OK]
- Thinking augmentation rewrites library code
- Confusing augmentation with code conversion
- Assuming augmentation removes functions
my-lib?Solution
Step 1: Recall correct augmentation syntax
TypeScript usesdeclare module 'library-name' { }to augment modules.Step 2: Check options for this syntax
Only declare module 'my-lib' { } usesdeclare modulecorrectly. Others use invalid keywords.Final Answer:
declare module 'my-lib' { } -> Option DQuick Check:
Use declare module for augmentation [OK]
- Omitting the declare keyword
- Using wrong keywords like augment or extend
- Using module without declare
lodash library to add a new method double to its LoDashStatic interface?declare module 'lodash' {
interface LoDashStatic {
double(n: number): number;
}
}
import _ from 'lodash';
console.log(_.double(4));Solution
Step 1: Understand augmentation effect
Augmentation adds type info but does not add actual implementation code.Step 2: Analyze runtime behavior
Since_.doubleis not implemented, calling it causes a runtime error.Final Answer:
Runtime error: _.double is not a function -> Option AQuick Check:
Augmentation adds types only, no code [OK]
- Assuming augmentation adds implementation automatically
- Expecting TypeScript error instead of runtime error
- Confusing type addition with code addition
express that tries to add a new property userId to the Request interface:declare module 'express' {
interface Request {
userId: string;
}
}
// Usage
import express from 'express';
const app = express();
app.use((req, res, next) => {
console.log(req.userId);
next();
});Solution
Step 1: Check correct module for Request interface
In Express, theRequestinterface is declared in 'express-serve-static-core', not directly in 'express'.Step 2: Understand augmentation target
To augmentRequest, you must declare module 'express-serve-static-core', or augmentation won't apply correctly.Final Answer:
The module name should be 'express-serve-static-core' instead of 'express' -> Option CQuick Check:
Augment correct module name for interface [OK]
- Augmenting wrong module name
- Making property optional unnecessarily
- Thinking interface cannot be augmented
react-router-dom library to add a new optional property meta of type { requiresAuth: boolean } to the RouteProps interface. Which code snippet correctly achieves this and ensures TypeScript recognizes the new property?Solution
Step 1: Identify correct module to augment
TheRoutePropsinterface is declared in 'react-router-dom', so we must augment that module.Step 2: Write augmentation with optional property
Usedeclare module 'react-router-dom'and addmeta?: { requiresAuth: boolean }insideRouteProps.Final Answer:
declare module 'react-router-dom' { interface RouteProps { meta?: { requiresAuth: boolean }; } } -> Option BQuick Check:
Augment correct module with optional property [OK]
- Augmenting wrong module name
- Not using declare module syntax
- Adding property outside interface
Ready to prove it?
Pass the test and this pattern moves unknown
