0
0
Typescriptprogramming~30 mins

Global augmentation in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Global Augmentation in TypeScript
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use console.log to print the boolean results of calling isOnlyVowels() on each string.