Extending TypeScript Global Object in NodeJS
— nodejs, typescript, global — 1 min read
You want to add a function to global
object in NodeJS
environment
type MyFunctionType = () => void;
global.myFunctionName = () => { console.log("hi");};
To extends global
object in TypeScript
, you need to augment the global
namespaces
declare global { namespace NodeJS { interface Global { myFunctionName: MyFunctionType } }}
With this in place, you now can call the function like this
global.myFunctionName();
To call the function without global.
, you need to augment the globalThis
type
declare global { namespace NodeJS { interface Global { myFunctionName: MyFunctionType } }
namespace globalThis { const myFunctionName: MyFunctionType }}
With this in place, you now can call the function like this
myFunctionName();
That's it. Thanks for reading.