I'm trying to add a global scope variable (which means it can be accessed from anywhere without having to import/require it from another file) in TypeScript.
If this is in JavaScript (NodeJS), it would look like this:
// index.js
globalThis.helloWorld = 'Hello World!';
require('./log.js')
// log.js
console.log(helloWorld);
// output
Hello World!
What I'm struggling against in TypeScript:
// types.ts
// ...
declare global{
var GLOBAL_EVENTS: EventEmitter
}
// ...
// app.ts
// ...
globalThis.GLOBAL_EVENTS = new EventEmitter()
// ...
// usage in another file
// ...
GLOBAL_EVENTS.on(...)
// ...
// output:
ReferenceError: GLOBAL_EVENTS is not defined
I've tried using global.GLOBAL_EVENTS too, but to no avail, it works. Where did I go wrong?