Bird
Raised Fist0
Typescriptprogramming~20 mins

Why module augmentation is needed in Typescript - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Module Augmentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use module augmentation in TypeScript?

Why do developers use module augmentation in TypeScript?

ATo delete existing types from a module to reduce its size.
BTo create a completely new module from scratch with unrelated functionality.
CTo add new properties or types to an existing module without modifying its original source code.
DTo convert JavaScript code into TypeScript automatically.
Attempts:
2 left
💡 Hint

Think about how you can extend existing code safely without changing the original files.

❓ Predict Output
intermediate
2:00remaining
Output of module augmentation example

What is the output of this TypeScript code using module augmentation?

Typescript
declare module 'math-lib' {
  export function multiply(x: number, y: number): number;
}

// Augmenting the module
declare module 'math-lib' {
  export function square(x: number): number;
}

// Implementation
const mathLib = {
  multiply: (x: number, y: number) => x * y,
  square: (x: number) => x * x
};

console.log(mathLib.multiply(3, 4));
console.log(mathLib.square(5));
A12\n25
B7\n25
C12\nundefined
DError: square is not a function
Attempts:
2 left
💡 Hint

Check how the augmented function square is defined and used.

🔧 Debug
advanced
2:00remaining
Identify the error in module augmentation usage

What error does this TypeScript code produce?

Typescript
declare module 'string-utils' {
  export function capitalize(s: string): string;
}

// Incorrect augmentation
declare module 'string-utils' {
  export const capitalize: (s: string) => string;
}

const utils = {
  capitalize: (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
};

console.log(utils.capitalize('hello'));
AType 'function' is not assignable to type 'const'.
BDuplicate identifier 'capitalize'.
CNo error, outputs 'Hello'.
DCannot redeclare block-scoped variable 'capitalize'.
Attempts:
2 left
💡 Hint

Look at how capitalize is declared in both module declarations.

📝 Syntax
advanced
2:00remaining
Correct syntax for module augmentation

Which option shows the correct syntax to augment a module named my-lib by adding a new interface User?

A
declare module my-lib {
  interface User {
    name: string;
    age: number;
  }
}
B
module 'my-lib' {
  interface User {
    name: string;
    age: number;
  }
}
C
augment module 'my-lib' {
  interface User {
    name: string;
    age: number;
  }
}
D
declare module 'my-lib' {
  interface User {
    name: string;
    age: number;
  }
}
Attempts:
2 left
💡 Hint

Remember the keyword to declare module augmentation and the quotes around module name.

🚀 Application
expert
3:00remaining
Why module augmentation is essential for third-party libraries

You want to add a new method formatDate to a third-party library's module date-lib without changing its source code. Why is module augmentation the best approach?

AIt allows adding new types and methods to the existing module safely, keeping compatibility and avoiding direct edits.
BIt automatically updates the third-party library to the latest version with new features.
CIt copies the entire library code so you can modify it freely.
DIt disables type checking for the module so you can add any code.
Attempts:
2 left
💡 Hint

Think about how to extend functionality without risking breaking the original library.

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

  1. 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.
  2. Step 2: Compare with other options

    Creating new modules, deleting modules, or converting files are unrelated to module augmentation's purpose.
  3. Final Answer:

    To add or fix types in existing modules without changing original code -> Option A
  4. 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

  1. Step 1: Recall the correct syntax for module augmentation

    The correct syntax uses the keyword declare module 'module-name' { } to add types.
  2. Step 2: Identify incorrect keywords

    Keywords like augment or extend are not valid TypeScript syntax for module augmentation.
  3. Final Answer:

    declare module 'module-name' { } -> Option B
  4. 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?
import 'some-library';
declare module 'some-library' {
  interface Options {
    debug: boolean;
  }
}

const opts: import('some-library').Options = { debug: true };
console.log(opts.debug);
medium
A. true
B. undefined
C. Type error: Property 'debug' does not exist
D. Runtime error

Solution

  1. Step 1: Understand module augmentation effect

    The code adds a new property debug to the Options interface inside 'some-library' module.
  2. Step 2: Check variable initialization and output

    The variable opts is assigned an object with debug: true, so logging opts.debug prints true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    Augmented property exists and logs true [OK]
Hint: Augmented types allow new properties without errors [OK]
Common Mistakes:
  • Expecting type error due to missing original property
  • Thinking output is undefined
  • Confusing runtime and compile-time errors
4. Identify the error in this module augmentation code:
declare module 'my-lib' {
  interface Config {
    timeout: number;
  }
}

const config: Config = { timeout: 5000 };
medium
A. Cannot assign number to timeout property
B. Syntax error in declare module statement
C. Missing semicolon after interface declaration
D. Config interface is not imported or globally available

Solution

  1. Step 1: Check scope of augmented interface

    The interface Config is declared inside module 'my-lib', so it is not globally visible.
  2. Step 2: Analyze usage of Config variable

    The variable config uses Config without importing or referencing the module, causing a type error.
  3. Final Answer:

    Config interface is not imported or globally available -> Option D
  4. 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

  1. 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.
  2. Step 2: Apply module augmentation

    Module augmentation with declare module 'module-name' lets you safely add logInfo() to the Logger interface inside the module.
  3. Final Answer:

    Use module augmentation with declare module and add logInfo() inside Logger interface -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard