Bird
Raised Fist0
Typescriptprogramming~20 mins

Global augmentation in Typescript - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of declare global in TypeScript?
easy
A. To import external modules into the current file
B. To declare a new local variable inside a function
C. To safely add or modify types on global objects or modules
D. To create a new class with global scope

Solution

  1. Step 1: Understand the role of declare global

    This keyword is used inside a module to extend or modify global types safely without changing original files.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated TypeScript features like local variables, imports, or classes.
  3. Final Answer:

    To safely add or modify types on global objects or modules -> Option C
  4. Quick Check:

    declare global = add/modify global types [OK]
Hint: Remember: declare global extends global types safely [OK]
Common Mistakes:
  • Confusing declare global with import statements
  • Thinking it declares local variables
  • Assuming it creates new classes
2. Which syntax correctly starts a global augmentation block inside a module in TypeScript?
easy
A. declare global { interface Window { customProp: string; } }
B. global declare { interface Window { customProp: string; } }
C. declare module global { interface Window { customProp: string; } }
D. module global declare { interface Window { customProp: string; } }

Solution

  1. Step 1: Identify correct syntax for global augmentation

    The correct syntax uses declare global { ... } inside a module to augment global types.
  2. Step 2: Check other options for syntax errors

    Options A, B, and C use incorrect order or keywords that are invalid in TypeScript.
  3. Final Answer:

    declare global { interface Window { customProp: string; } } -> Option A
  4. Quick Check:

    declare global { ... } is correct syntax [OK]
Hint: Use 'declare global { }' exactly to augment globals [OK]
Common Mistakes:
  • Swapping 'declare' and 'global' keywords
  • Using 'declare module global' incorrectly
  • Missing curly braces after declare global
3. What will be the output of this TypeScript code after global augmentation?
export {};
declare global {
  interface Array<T> {
    first(): T | undefined;
  }
}

Array.prototype.first = function() {
  return this.length > 0 ? this[0] : undefined;
};

const nums = [10, 20, 30];
console.log(nums.first());
medium
A. undefined
B. 30
C. Error: Property 'first' does not exist on type 'number[]'
D. 10

Solution

  1. Step 1: Understand the global augmentation of Array interface

    The code adds a new method first() to all arrays returning the first element or undefined if empty.
  2. Step 2: Analyze the output of calling nums.first()

    The array nums has elements [10, 20, 30], so first() returns 10, the first element.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    nums.first() = 10 [OK]
Hint: Augmented methods work like normal after declaration [OK]
Common Mistakes:
  • Expecting a TypeScript error without module context
  • Thinking first() returns undefined always
  • Confusing first element with last element
4. Identify the error in this global augmentation code and fix it:
declare global {
  interface String {
    shout(): string;
  }
}

String.prototype.shout = function() {
  return this.toUpperCase() + "!";
};
medium
A. Cannot augment String interface globally
B. Missing export {} to make file a module
C. Incorrect method name, should be 'shoutOut'
D. Prototype assignment must be inside declare global block

Solution

  1. Step 1: Check module context requirement for global augmentation

    Global augmentation requires the file to be a module, so it needs an export {} or import statement.
  2. Step 2: Identify missing export statement

    The code lacks export {}, so TypeScript treats it as a script, causing errors with declare global.
  3. Final Answer:

    Missing export {} to make file a module -> Option B
  4. Quick Check:

    File must be a module for declare global [OK]
Hint: Add 'export {}' to enable global augmentation [OK]
Common Mistakes:
  • Forgetting to make file a module
  • Trying to assign prototype inside declare global
  • Renaming methods unnecessarily
5. You want to add a global method logInfo() to the console object that prefixes messages with '[INFO]'. Which is the correct way to augment the global Console interface and implement this method?
hard
A. export {}; declare global { interface Console { logInfo(message: string): void; } } console.logInfo = (msg) => console.log(`[INFO] ${msg}`);
B. declare module 'console' { interface Console { logInfo(message: string): void; } } console.logInfo = (msg) => console.log(`[INFO] ${msg}`);
C. declare global { interface Console { logInfo(message: string): void; } } console.prototype.logInfo = function(msg) { console.log(`[INFO] ${msg}`); };
D. export {}; declare global { interface Console { logInfo(message: string): void; } } console.prototype.logInfo = (msg) => console.log(`[INFO] ${msg}`);

Solution

  1. Step 1: Correctly augment the global Console interface

    Use export {} to make the file a module, then declare global with interface Console adding logInfo.
  2. Step 2: Properly assign the method to console object

    Assign console.logInfo directly, not on console.prototype because console is an object, not a class instance.
  3. Final Answer:

    export {}; declare global { interface Console { logInfo(message: string): void; } } console.logInfo = (msg) => console.log(`[INFO] ${msg}`); -> Option A
  4. Quick Check:

    Use declare global + direct console method assignment [OK]
Hint: Augment interface then assign method directly on console [OK]
Common Mistakes:
  • Using declare module 'console' instead of declare global
  • Assigning method to console.prototype (wrong for object)
  • Forgetting export {} to make file a module
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard