0
0
Typescriptprogramming~20 mins

Augmenting third-party libraries in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Augmentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code with module augmentation?

Consider the following TypeScript code that augments a third-party library's interface. What will be logged to the console?

Typescript
import 'lodash';

declare module 'lodash' {
  interface LoDashStatic {
    customMethod(): string;
  }
}

import _ from 'lodash';

_.customMethod = () => 'Augmented!';

console.log(_.customMethod());
ATypeScript compile error: Property 'customMethod' does not exist on type 'LoDashStatic'.
B"undefined"
CRuntime error: _.customMethod is not a function
D"Augmented!"
Attempts:
2 left
💡 Hint

Think about how module augmentation adds new members to existing interfaces.

🧠 Conceptual
intermediate
1:30remaining
Which statement about TypeScript module augmentation is true?

Choose the correct statement about augmenting third-party libraries in TypeScript.

AModule augmentation automatically merges runtime implementations without extra code.
BModule augmentation can add new properties to existing interfaces without modifying original source code.
CModule augmentation only works with classes, not interfaces.
DModule augmentation requires changing the original library's source files.
Attempts:
2 left
💡 Hint

Think about how TypeScript allows extending types without touching original files.

🔧 Debug
advanced
2:30remaining
Why does this module augmentation cause a TypeScript error?

Given this code snippet, why does TypeScript report an error?

Typescript
import 'express';

declare module 'express' {
  interface Request {
    userId: number;
  }
}

import express from 'express';
const app = express();

app.use((req, res, next) => {
  console.log(req.userId);
  next();
});
AThe 'userId' property is not optional, causing runtime errors.
BThe module 'express' does not export a 'Request' interface to augment.
CThe augmentation is missing an import of 'express' before declaration.
DThe augmentation syntax is invalid; interfaces cannot be augmented.
Attempts:
2 left
💡 Hint

Check if the module is imported before augmentation.

📝 Syntax
advanced
2:00remaining
Which option correctly augments a third-party library's namespace in TypeScript?

Choose the correct syntax to add a new function logInfo to the console namespace.

A
declare global {
  namespace console {
    function logInfo(message: string): void;
  }
}
B
declare module 'console' {
  export function logInfo(message: string): void;
}
C
declare namespace console {
  function logInfo(message: string): void;
}
D
declare interface console {
  logInfo(message: string): void;
}
Attempts:
2 left
💡 Hint

Think about how to augment global namespaces in TypeScript.

🚀 Application
expert
3:00remaining
After augmenting a third-party library, what is the type of this variable?

Given the following augmentation and usage, what is the type of result?

Typescript
import 'moment';

declare module 'moment' {
  interface Moment {
    isWeekend(): boolean;
  }
}

import moment from 'moment';

const now = moment();
const result = now.isWeekend();
Aboolean
BMoment
Cundefined
DTypeScript error: Property 'isWeekend' does not exist on type 'Moment'.
Attempts:
2 left
💡 Hint

Consider the return type of the added method in the interface.