0
0
Typescriptprogramming~20 mins

Why module augmentation is needed in Typescript - Challenge Your Understanding

Choose your learning style9 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.