How to extend the Node.js Global (globalThis) object in TypeScript

Extend the Node.js Global (globalThis) object in TypeScript

If you are looking for a method to extend the Node.js Global (globalThis) object in TypeScript, then this article is just what you need. We will provide you with the procedure to extend Node.js Global through this tutorial. Check out the information below for detailed instructions.

Extend the Node.js Global (globalThis) object in TypeScript

You can follow the steps below to extend the Node.js Global (globalThis) object in TypeScript:

  • First, you need to create a file with the extension ‘.d.ts’ in your project. This file is located in the same place as the regular ‘.ts’.
  • Use the ‘declare global{}’ statement for extending the global object with typings for the essential methods and properties.

To illustrate our method, you can take a look at this example program:

Here we create an ‘index.d.ts’ file. TypeScript will find this file in the exact place it finds other ‘.ts’ files. You need to pay attention to the location of the files when working with them to avoid unexpected errors.

interface Numbers {
    primeNumber: number;
    non_primeNumber: number;
}

declare global {
    var myObject: Numbers;
    function isPrime(n: number): boolean;
}

export {};

In this example, we show you the method to extend the global object (globalThis) with the ‘myObject’ property, an object of type Numbers, and the isPrime() function.

Remember to use the ‘var’ keyword to add typing for the properties you plan to work with.

Moreover, you must also add the types and names of all the properties you plan to access on the global object.

At this point, we can work with the specified global object property without errors. Here is the code in the ‘index.ts’ file:

global.myObject = {
    primeNumber: 11,
    non_primeNumber: 10,
};

console.log("Prime number: " + global.myObject.primeNumber);
console.log("Non-prime number: " + global.myObject.non_primeNumber);

global.isPrime = (n: number) => {
    // Variable that stores the result
    var flag = true;
  
    // if n is less than 2, it is not prime
    if (n < 2) {
        flag = false;
    } else {
      	// Loop from 2 to n-1
        for (var i = 2; i < n - 1; i++) {
            if (n % i == 0) {
                flag = false;
                break;
            }
        }
    }
    return flag;
};

console.log(global.isPrime(global.myObject.primeNumber));
console.log(global.isPrime(global.myObject.non_primeNumber));

After successfully executing the ‘index.ts’ file, you will get the following output:

Prime number: 11
Non-prime number: 10
true
false

In case you are unsure about a particular property type and want to disable type checking. You can set it to type ‘any’.

interface Numbers {
    primeNumber: number;
    non_primeNumber: number;
}

declare global {
    var myObject: any; // Use the 'any' keyword to type the property to type 'any'
    function isPrime(n: number): boolean;
}

export {};

Then you can safely use it without worrying about the attribute type.

Summary

To sum up, we have shown you how to extend the Node.js Global (globalThis) object in TypeScript through the detailed instructions above. That’s the end of this tutorial. Hopefully, this article will be helpful to you. Try to practice along and see the results obtained to get a better understanding.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *