0
0
Typescriptprogramming~20 mins

Module augmentation syntax in Typescript - Practice Problems & Coding Challenges

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!
Predict Output
intermediate
2: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);
ATypeError: Cannot read property 'name' of undefined
B{"name":"Alice","age":30}
CSyntaxError: Duplicate identifier 'User'
D{"name":"Alice"}
Attempts:
2 left
💡 Hint
Think about how TypeScript merges interfaces in module augmentation.
🧠 Conceptual
intermediate
1:30remaining
Purpose of module augmentation syntax
What is the main purpose of using module augmentation syntax in TypeScript?
ATo add new exports or extend existing types in an external module without modifying its original source code
BTo import all exports from a module under a new namespace
CTo create a new module with the same name as an existing one, replacing it completely
DTo declare global variables accessible in all modules
Attempts:
2 left
💡 Hint
Think about extending functionality without changing original files.
🔧 Debug
advanced
2: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);
ACannot redeclare exported function 'greet' without implementation
BModule 'library' has no exported member 'version'
CDuplicate identifier 'library'
DNo error, code compiles and runs fine
Attempts:
2 left
💡 Hint
Check if the augmentation declares implementations or only types.
📝 Syntax
advanced
1: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?
A
declare module express {
  interface Request {
    userId: string;
  }
}
B
module "express" {
  interface Request {
    userId: string;
  }
}
C
augment module "express" {
  interface Request {
    userId: string;
  }
}
D
declare module "express" {
  interface Request {
    userId: string;
  }
}
Attempts:
2 left
💡 Hint
Remember the keyword to declare module augmentation.
🚀 Application
expert
3: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 };
AConfig has only the last declared property: { port: number }
BConfig has only the first declared property: { host: string }
CConfig has both properties: { host: string; port: number }
DConfig is an empty interface with no properties
Attempts:
2 left
💡 Hint
Think about how TypeScript merges interfaces with the same name in module augmentation.