Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose TypeScript variable to JS global scope (with webpack) [duplicate]

I'm using webpack to compile my TypeScript to Javascript and I want to expose a variable to the global scope so others can access it.

I know how to do the following in TypeScript

window["MyVariable"] = 'Hello';

and read it in JavaScript like that

console.log(window.MyVariable);

But I'd prefer if I could just have it in the global scope like that.

console.log(MyVariable);

Is this possible?

Best regards, Nick

like image 832
Nick Avatar asked Oct 23 '25 21:10

Nick


1 Answers

Whatever exists on the global scope can be accessed without window. in JavaScript. So if you set window['MyVariable'] = 'Hello', you can access it in JavaScript simply with MyVariable.

like image 138
rid Avatar answered Oct 26 '25 09:10

rid