📖 Scenario: You are using a popular third-party library that provides a User interface. You want to add a new property to this interface without modifying the original library code.
🎯 Goal: Learn how to safely add new properties to existing third-party interfaces using TypeScript declaration merging.
📋 What You'll Learn
Create an interface augmentation for the third-party User interface
Add a new property isAdmin of type boolean
Create a variable of type User including the new property
Print the new property value
💡 Why This Matters
🌍 Real World
Many libraries provide interfaces you cannot change directly. Augmenting lets you add your own properties safely.
💼 Career
Understanding how to extend third-party types is important for working with large codebases and popular libraries in TypeScript.
Progress0 / 4 steps
1
Create the original User interface
Create an interface called User with properties name (string) and email (string).
Typescript
Hint
Use interface User { name: string; email: string; } to define the interface.
2
Augment the User interface with isAdmin
Add a new property isAdmin of type boolean to the existing User interface using declaration merging.
Typescript
Hint
Write interface User { isAdmin: boolean; } separately to add the new property.
3
Create a User object including isAdmin
Create a variable called user of type User with name set to "Alice", email set to "alice@example.com", and isAdmin set to true.
Typescript
Hint
Use const user: User = { name: "Alice", email: "alice@example.com", isAdmin: true };.
4
Print the isAdmin property
Write a console.log statement to print the value of user.isAdmin.
Typescript
Hint
Use console.log(user.isAdmin); to print the value.
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.