What does it mean to augment a third-party library in TypeScript?
It means adding new types or properties to existing types from a library without changing the original source code. This helps customize or extend library features safely.
Click to reveal answer
beginner
How do you start augmenting a module in TypeScript?
You use the declare module 'module-name' syntax to reopen the module and add new types or interfaces inside it.
Click to reveal answer
beginner
Why should you avoid modifying third-party library source code directly?
Because updates to the library can overwrite your changes, and it can cause maintenance problems. Augmentation keeps your changes separate and safe.
Click to reveal answer
intermediate
What TypeScript feature allows you to add properties to existing interfaces?
Interface merging lets you add new properties by declaring the same interface name again with extra members.
Click to reveal answer
intermediate
Give an example of augmenting the 'express' Request interface to add a new property 'userId'.
declare module 'express' {
interface Request {
userId?: string;
}
}
This adds an optional 'userId' property to the Request object.
Click to reveal answer
Which keyword is used to reopen a module for augmentation in TypeScript?
Adeclare module
Bimport module
Cexport module
Dmodule augment
✗ Incorrect
You use declare module 'module-name' to reopen and augment a module.
What happens if you modify a third-party library source code directly?
AIt is the recommended way
BIt automatically merges with your code
CTypeScript will prevent it
DYour changes may be lost on library updates
✗ Incorrect
Direct changes can be overwritten when the library updates, causing loss of your work.
How can you add new properties to an existing interface in TypeScript?
ABy extending the interface in a new file
BBy redeclaring the interface with the same name
CBy copying and modifying the interface
DBy using the 'override' keyword
✗ Incorrect
TypeScript merges interfaces with the same name, allowing you to add properties.
Which of these is a safe way to add types to a third-party library?
ACopying library code
BEditing node_modules files
CModule augmentation
DIgnoring type errors
✗ Incorrect
Module augmentation safely extends types without changing original code.
In the example declare module 'express' { interface Request { userId?: string; } }, what does the question mark mean?
AThe property is optional
BThe property is required
CThe property is private
DThe property is readonly
✗ Incorrect
The question mark marks the property as optional.
Explain how to augment a third-party library in TypeScript and why it is useful.
Think about reopening modules and adding new properties without touching original files.
You got /4 concepts.
Describe a real-life scenario where augmenting a third-party library would help your project.
Imagine you want to add user info to a request object from a web framework.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of augmenting third-party libraries in TypeScript?
easy
A. To add or fix types without changing the original library code
B. To rewrite the entire library code for better performance
C. To remove unused functions from the library
D. To convert JavaScript code to TypeScript automatically
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 A
Quick Check:
Augmenting = add/fix types without code change [OK]
Hint: Augmenting means adding types, not changing code [OK]
Common Mistakes:
Thinking augmentation rewrites library code
Confusing augmentation with code conversion
Assuming augmentation removes functions
2. Which syntax correctly starts a module augmentation for a library named my-lib?
easy
A. extend module 'my-lib' { }
B. module 'my-lib' { }
C. augment module 'my-lib' { }
D. declare module 'my-lib' { }
Solution
Step 1: Recall correct augmentation syntax
TypeScript uses declare module 'library-name' { } to augment modules.
Step 2: Check options for this syntax
Only declare module 'my-lib' { } uses declare module correctly. Others use invalid keywords.
Final Answer:
declare module 'my-lib' { } -> Option D
Quick Check:
Use declare module for augmentation [OK]
Hint: Always start with declare module 'library-name' [OK]
Common Mistakes:
Omitting the declare keyword
Using wrong keywords like augment or extend
Using module without declare
3. What will be the output of this TypeScript code after augmenting the lodash library to add a new method double to its LoDashStatic interface?
A. The import statement should use require instead of import
B. The property userId must be optional with a question mark
C. The module name should be 'express-serve-static-core' instead of 'express'
D. The interface Request cannot be augmented
Solution
Step 1: Check correct module for Request interface
In Express, the Request interface is declared in 'express-serve-static-core', not directly in 'express'.
Step 2: Understand augmentation target
To augment Request, 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 C
Quick Check:
Augment correct module name for interface [OK]
Hint: Augment the exact module where interface is declared [OK]
Common Mistakes:
Augmenting wrong module name
Making property optional unnecessarily
Thinking interface cannot be augmented
5. You want to augment the 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?
hard
A. interface RouteProps { meta?: { requiresAuth: boolean }; }
// No module declaration needed
B. declare module 'react-router-dom' { interface RouteProps { meta?: { requiresAuth: boolean }; } }
// Then use
C. declare module 'react-router' { interface RouteProps { meta?: { requiresAuth: boolean }; } }
// Wrong module name
D. declare global { interface RouteProps { meta?: { requiresAuth: boolean }; } }
// Incorrect global augmentation
Solution
Step 1: Identify correct module to augment
The RouteProps interface is declared in 'react-router-dom', so we must augment that module.
Step 2: Write augmentation with optional property
Use declare module 'react-router-dom' and add meta?: { requiresAuth: boolean } inside RouteProps.