26

How to explicitly declare a variable in the global namespace from typescript?

I need the compiler to generate the following javascript code:

MyExtension = someFunction()

unfortunately, I can only have it generate

var MyExtension = someFunction()

This comes to an issue with the latest version (still in rc) of meteor packages. Meteor introduced a way to scope namespaces in packages - the issue is, the variable needs to be defined in the global namespace (which meteor reroutes to its own Package object).

There is a video about it at https://www.eventedmind.com/posts/meteor-linker-package-namespacing.

Is there some kind of global keyword available or in the plans?

1 Answer 1

71

Use the declare keyword. These are known as ambient declarations.

declare var MyExtentention:any;
Sign up to request clarification or add additional context in comments.

1 Comment

In my experience, this has to go at the top of the file. If you place this inside another variable declaration, e.g., near where you are using the global variable, the compiler will complain that "Modifiers cannot appear here".

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.