📖 Scenario: Imagine you are working on a TypeScript project where you want to add a new method to the built-in String type. This method will help you check if a string contains only vowels. Since you cannot change the original String type directly, you will use global augmentation to add this new method.
🎯 Goal: You will create a global augmentation for the String interface by adding a method called isOnlyVowels(). This method will return true if the string contains only vowels (a, e, i, o, u, case insensitive), and false otherwise. You will then test this method with some example strings.
📋 What You'll Learn
Create a global augmentation for the String interface
Add a method isOnlyVowels() that returns a boolean
Implement the isOnlyVowels() method to check if the string contains only vowels
Test the method with example strings and print the results
💡 Why This Matters
🌍 Real World
Global augmentation is useful when you want to extend existing libraries or built-in types without modifying their source code. For example, adding helpful utility methods to strings or arrays.
💼 Career
Many TypeScript projects require extending third-party types or built-in types safely. Knowing global augmentation helps you customize and improve code reuse while keeping type safety.
Progress0 / 4 steps
1
Create a global augmentation for the String interface
Write a declare global block and inside it, add an interface String with a method isOnlyVowels(): boolean.
Typescript
Hint
Use declare global to add new members to existing global types.
2
Implement the isOnlyVowels method on String prototype
Add an implementation for isOnlyVowels on String.prototype that returns true if the string contains only vowels (a, e, i, o, u, case insensitive), otherwise false.
Typescript
Hint
Use a regular expression to check if the string contains only vowels.
3
Create example strings to test the isOnlyVowels method
Create three string variables: str1 with value "aeiou", str2 with value "hello", and str3 with value "AEIOU".
Typescript
Hint
Use const to create the string variables with the exact values.
4
Print the results of calling isOnlyVowels on the example strings
Write three console.log statements to print the results of str1.isOnlyVowels(), str2.isOnlyVowels(), and str3.isOnlyVowels().
Typescript
Hint
Use console.log to print the boolean results of calling isOnlyVowels() on each string.
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
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.
Step 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 C
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
Step 1: Identify correct syntax for global augmentation
The correct syntax uses declare 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 A
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?
D. Prototype assignment must be inside declare global block
Solution
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.
Step 2: Identify missing export statement
The code lacks export {}, so TypeScript treats it as a script, causing errors with declare global.
Final Answer:
Missing export {} to make file a module -> Option B
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}`);