3

If I have a global variable var x in my main.js file, which i'd like to be accessible in a typescript file which is bundled by webpack into the same build/bundle.js, how would i go about this? I cannot access it directly as i assumed i might (them being in the same bundle at the end). I have read of export/import/require but cannot find how to apply this in the context of a webpack bundle.

2 Answers 2

4

You may need to declare the variable in your typescript file so the compiler knows its exists:

declare var x: any;

Adapted from this answer

Sign up to request clarification or add additional context in comments.

Comments

0

ProvidePlugin from webpack should do the job, they have a pretty good example here

2 Comments

I don't understand how to do this with my own file referring to the documentation's examples
just to clarify my understanding of your problem, currently with your global variable x, do you mean typescript compiler is simply complaining that you are trying to access an undefined variable hence not compiling (but functionally works if you do something like window['x']. or are you saying that the variable is not being globally scoped but file scoped instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.