Categories
TypeScript Answers

How to extend TypeScript global object in Node.js?

Spread the love

To extend TypeScript global object in Node.js, we can add a type definition file that extends the Global interface in the NodeJS module.

For instance, we write

declare module NodeJS {
  interface Global {
    foo: any;
  }
}

in a .d.ts file in our Node TypeScript project to add the foo property into the Global interface.

Then we can use the variable without compiler errors by using global.foo.

Conclusion

To extend TypeScript global object in Node.js, we can add a type definition file that extends the Global interface in the NodeJS module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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