Challenge - 5 Problems
Global Augmentation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code with global augmentation?
Consider the following TypeScript code that augments the global Window interface. What will be logged to the console?
Typescript
declare global {
interface Window {
customMessage: string;
}
}
window.customMessage = "Hello, world!";
console.log(window.customMessage);Attempts:
2 left
💡 Hint
Think about how global interface augmentation adds properties to existing types.
✗ Incorrect
The global augmentation adds the 'customMessage' property to the Window interface, so assigning and accessing it works without errors.
❓ Predict Output
intermediate1:30remaining
What error occurs when accessing an unaugmented global property?
Given this code, what error will TypeScript produce?
Typescript
console.log(window.newProperty); // No global augmentation for 'newProperty' is declared.
Attempts:
2 left
💡 Hint
Check if the property is declared in the Window interface.
✗ Incorrect
TypeScript checks types at compile time and will error if you access a property not declared on the Window interface.
🔧 Debug
advanced2:30remaining
Why does this global augmentation cause a compilation error?
Examine the following code. Why does TypeScript report an error on the augmentation?
Typescript
declare global {
interface Window {
customMethod(): void;
}
}
window.customMethod = () => {
console.log("Hi");
};Attempts:
2 left
💡 Hint
Check the difference between method declarations and property assignments.
✗ Incorrect
Declaring 'customMethod' as a method requires assigning a function, and assigning an arrow function is valid.
📝 Syntax
advanced2:30remaining
Which option correctly augments the global NodeJS Process interface?
You want to add a new property 'isDebugMode' of type boolean to the global NodeJS Process interface. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Remember the NodeJS Process interface is inside the NodeJS namespace.
✗ Incorrect
The Process interface is inside the NodeJS namespace, so global augmentation must declare the namespace and interface inside it.
🚀 Application
expert3:00remaining
How many properties does the augmented global interface have after this code runs?
Given this global augmentation and code, how many enumerable own properties does the object 'window' have after execution?
Typescript
declare global {
interface Window {
propA: number;
propB: string;
}
}
window.propA = 10;
window.propB = "test";
window.propC = true;
const keys = Object.keys(window);Attempts:
2 left
💡 Hint
Think about which properties are actually assigned and enumerable on the window object.
✗ Incorrect
Even though 'propC' is not declared in the interface, it is assigned at runtime and is enumerable. So keys includes 'propA', 'propB', and 'propC'.