Global augmentation lets you add new things to existing global types or modules. It helps you customize or extend code without changing original files.
Global augmentation in Typescript
declare global {
interface ExistingGlobalInterface {
newProperty: string;
}
}
export {}You must use declare global inside a module (a file with at least one import or export).
Always add export {} at the end to make the file a module.
myCustomValue to the global Window object.declare global {
interface Window {
myCustomValue: number;
}
}
export {}first() to all arrays to get the first item safely.declare global {
interface Array<T> {
first(): T | undefined;
}
}
Array.prototype.first = function() {
return this.length > 0 ? this[0] : undefined;
};
export {}myGlobalFlag with boolean type.declare global {
var myGlobalFlag: boolean;
}
export {}This program adds a new property appVersion to the global window object and then prints it.
declare global {
interface Window {
appVersion: string;
}
}
window.appVersion = "1.0.3";
console.log(`App version is ${window.appVersion}`);
export {}Global augmentation only works inside modules, so add export {} if needed.
Be careful not to overwrite existing types or properties accidentally.
Use global augmentation to improve type safety and avoid modifying original library code.
Global augmentation lets you add or change types on global objects or modules.
Use declare global inside a module to do this safely.
This helps customize or fix types without changing original code files.
Practice
declare global in TypeScript?Solution
Step 1: Understand the role of
This keyword is used inside a module to extend or modify global types safely without changing original files.declare globalStep 2: Compare with other options
Options B, C, and D describe unrelated TypeScript features like local variables, imports, or classes.Final Answer:
To safely add or modify types on global objects or modules -> Option CQuick Check:
declare global = add/modify global types [OK]
- Confusing declare global with import statements
- Thinking it declares local variables
- Assuming it creates new classes
Solution
Step 1: Identify correct syntax for global augmentation
The correct syntax usesdeclare global { ... }inside a module to augment global types.Step 2: Check other options for syntax errors
Options A, B, and C use incorrect order or keywords that are invalid in TypeScript.Final Answer:
declare global { interface Window { customProp: string; } } -> Option AQuick Check:
declare global { ... } is correct syntax [OK]
- Swapping 'declare' and 'global' keywords
- Using 'declare module global' incorrectly
- Missing curly braces after declare global
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());Solution
Step 1: Understand the global augmentation of Array interface
The code adds a new methodfirst()to all arrays returning the first element or undefined if empty.Step 2: Analyze the output of calling
The arraynums.first()numshas elements [10, 20, 30], sofirst()returns 10, the first element.Final Answer:
10 -> Option DQuick Check:
nums.first() = 10 [OK]
- Expecting a TypeScript error without module context
- Thinking first() returns undefined always
- Confusing first element with last element
declare global {
interface String {
shout(): string;
}
}
String.prototype.shout = function() {
return this.toUpperCase() + "!";
};Solution
Step 1: Check module context requirement for global augmentation
Global augmentation requires the file to be a module, so it needs anexport {}or import statement.Step 2: Identify missing export statement
The code lacksexport {}, so TypeScript treats it as a script, causing errors withdeclare global.Final Answer:
Missing export {} to make file a module -> Option BQuick Check:
File must be a module for declare global [OK]
- Forgetting to make file a module
- Trying to assign prototype inside declare global
- Renaming methods unnecessarily
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?Solution
Step 1: Correctly augment the global Console interface
Useexport {}to make the file a module, thendeclare globalwith interface Console addinglogInfo.Step 2: Properly assign the method to console object
Assignconsole.logInfodirectly, not onconsole.prototypebecause console is an object, not a class instance.Final Answer:
export {}; declare global { interface Console { logInfo(message: string): void; } } console.logInfo = (msg) => console.log(`[INFO] ${msg}`); -> Option AQuick Check:
Use declare global + direct console method assignment [OK]
- Using declare module 'console' instead of declare global
- Assigning method to console.prototype (wrong for object)
- Forgetting export {} to make file a module
Ready to prove it?
Pass the test and this pattern moves unknown
