Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare global namespace variable from TypeScript

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?

like image 269
Olivier Refalo Avatar asked Aug 11 '13 12:08

Olivier Refalo


People also ask

How do I declare a variable globally in TypeScript?

To declare a global variable in TypeScript, create a . d. ts file and use declare global{} to extend the global object with typings for the necessary properties or methods.

How do you define a namespace in TypeScript?

The namespace is used for logical grouping of functionalities. A namespace can include interfaces, classes, functions and variables to support a single or a group of related functionalities. A namespace can be created using the namespace keyword followed by the namespace name.

Where do you declare global type variables?

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.


Video Answer


1 Answers

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

declare var MyExtentention:any; 
like image 171
basarat Avatar answered Oct 17 '22 14:10

basarat