0
0
Typescriptprogramming~20 mins

Global augmentation in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Global 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 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);
ACompilation error: Property 'customMessage' does not exist on type 'Window'
BTypeError: Cannot set property 'customMessage' of undefined
C"Hello, world!"
Dundefined
Attempts:
2 left
💡 Hint
Think about how global interface augmentation adds properties to existing types.
Predict Output
intermediate
1: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.
ACompilation error: Property 'newProperty' does not exist on type 'Window'
Bundefined
CRuntime error: Cannot read property 'newProperty' of undefined
DNo error, prints 'null'
Attempts:
2 left
💡 Hint
Check if the property is declared in the Window interface.
🔧 Debug
advanced
2: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");
};
AError because 'window' is not declared in this scope
BError because global augmentation must be inside a module
CError because 'customMethod' is declared as a method but assigned as a property without a function type
DNo error, code is valid
Attempts:
2 left
💡 Hint
Check the difference between method declarations and property assignments.
📝 Syntax
advanced
2: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?
A
declare global {
  namespace NodeJS {
    interface Process {
      isDebugMode: boolean;
    }
  }
}
B
declare module 'process' {
  interface Process {
    isDebugMode: boolean;
  }
}
C
declare global {
  interface Process {
    isDebugMode: boolean;
  }
}
D
interface Process {
  isDebugMode: boolean;
}
Attempts:
2 left
💡 Hint
Remember the NodeJS Process interface is inside the NodeJS namespace.
🚀 Application
expert
3: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);
A0
B3
C2
DCannot determine
Attempts:
2 left
💡 Hint
Think about which properties are actually assigned and enumerable on the window object.