Challenge - 5 Problems
Module Augmentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of module augmentation with interface merging
What is the output of this TypeScript code when compiled and run with Node.js (using ts-node or similar)?
Typescript
declare module "my-module" { interface User { name: string; } } // Augmenting the module import "my-module"; declare module "my-module" { interface User { age: number; } } const user: import("my-module").User = { name: "Alice", age: 30 }; console.log(user);
Attempts:
2 left
💡 Hint
Think about how TypeScript merges interfaces in module augmentation.
✗ Incorrect
TypeScript merges interface declarations with the same name inside module augmentation. The User interface will have both 'name' and 'age' properties. The object matches the merged interface, so the output is the full object with both properties.
🧠 Conceptual
intermediate1:30remaining
Purpose of module augmentation syntax
What is the main purpose of using module augmentation syntax in TypeScript?
Attempts:
2 left
💡 Hint
Think about extending functionality without changing original files.
✗ Incorrect
Module augmentation allows you to add new types or members to an existing module's declarations, useful for extending third-party modules safely.
🔧 Debug
advanced2:30remaining
Identify the error in this module augmentation
This code tries to augment a module but causes a compilation error. What is the error?
Typescript
declare module "library" { export function greet(): void; } declare module "library" { export const version: string; } // Usage import { greet, version } from "library"; greet(); console.log(version);
Attempts:
2 left
💡 Hint
Check if the augmentation declares implementations or only types.
✗ Incorrect
Module augmentation can only add types or declarations, but cannot redeclare exported functions without implementation. The first declaration declares a function without implementation, causing an error.
📝 Syntax
advanced1:30remaining
Correct syntax for module augmentation
Which option shows the correct syntax to augment the 'express' module by adding a new property 'userId' of type string to the Request interface?
Attempts:
2 left
💡 Hint
Remember the keyword to declare module augmentation.
✗ Incorrect
The correct syntax uses 'declare module "module-name"' with quotes around the module name. Other options miss 'declare' or quotes or use invalid keywords.
🚀 Application
expert3:00remaining
Resulting type after multiple module augmentations
Given these two separate module augmentation files for module 'config', what is the type of Config after both are loaded?
Typescript
// File 1 declare module "config" { interface Config { host: string; } } // File 2 declare module "config" { interface Config { port: number; } } // Usage import type { Config } from "config"; const serverConfig: Config = { host: "localhost", port: 8080 };
Attempts:
2 left
💡 Hint
Think about how TypeScript merges interfaces with the same name in module augmentation.
✗ Incorrect
TypeScript merges interface declarations with the same name inside the same module, so Config will have both 'host' and 'port' properties.