Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript global variables

Tags:

Is there a convenient way to have global variables accessible within modules,without compiler errors, ie CANVAS_WIDTH used below?

    export class Bullet {           x: number = 22;         y: number = 22;          constructor (speed: number) {             this.xVelocity = speed;         }          inBounds() {             return this.x >= 0 && this.x <= CANVAS_WIDTH &&                 this.y >= 0 && this.y <= CANVAS_HEIGHT;         }; } } 
like image 683
Nikos Avatar asked Dec 11 '12 11:12

Nikos


People also ask

Does TypeScript have global variables?

Another way to declare global variables in TypeScript is to extend the built-in Window interface. Instead of using the declare keyword, we need to define an interface called Window , and add our global types on top of it.

How do I store a global variable 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 I create a global variable in TypeScript class?

#Declare a Global Variable Another approach is to declare a global variable using the declare var syntax. This way, we can let TypeScript know that it can expect to find a global variable with the given name and type: declare var __INITIAL_DATA__: InitialData; We can now access the __INITIAL_DATA__ variable directly …


2 Answers

You need to define those properties as static, then you can access it easily like this,

export class Game {     static canvas: JQuery;     static CANVAS_WIDTH: number;     static CANVAS_HEIGHT: number;     bullet: Bullet;      constructor(canvasElem: JQuery) {         Game.canvas = canvasElem;         Game.CANVAS_WIDTH = Game.canvas.width();         Game.CANVAS_HEIGHT = Game.canvas.height();     } }  export class Bullet {     x: number = 22;     y: number = 22;      public inBounds() {         // accessing static properties         return this.x >= 0 && this.x <= Game.CANVAS_WIDTH && this.y >= 0 && this.y <= Game.CANVAS_HEIGHT;     } } 

This compiles to:

define(["require", "exports"], function(require, exports) {     var Game = (function () {         function Game(canvasElem) {             Game.canvas = canvasElem;             Game.CANVAS_WIDTH = Game.canvas.width();             Game.CANVAS_HEIGHT = Game.canvas.height();         }         return Game;     })();     exports.Game = Game;      var Bullet = (function () {         function Bullet() {             this.x = 22;             this.y = 22;         }         Bullet.prototype.inBounds = function () {             // accessing static properties             return this.x >= 0 && this.x <= Game.CANVAS_WIDTH && this.y >= 0 && this.y <= Game.CANVAS_HEIGHT;         };         return Bullet;     })();     exports.Bullet = Bullet; }); //# sourceMappingURL=dhdh.js.map 
like image 200
Rajagopal 웃 Avatar answered Oct 07 '22 18:10

Rajagopal 웃


This is a contrived example, but rather than trying to push to global scope, you can use the module scope to enclose a variable that will be used from several classes.

module MyModule {     var x: number = 5;      export class FirstClass {         doSomething() {             x = 10;         }     }      export class SecondClass {         showSomething() {             alert(x.toString());         }     } }  var a = new MyModule.FirstClass(); a.doSomething();  var b = new MyModule.SecondClass(); b.showSomething(); 

All the usual rules about multiple things using the same variable apply here - you don't want to enforce a particular order of events on the calling code.


Compiles to:

var MyModule; (function (MyModule) {     var x = 5;      var FirstClass = (function () {         function FirstClass() {         }         FirstClass.prototype.doSomething = function () {             x = 10;         };         return FirstClass;     })();     MyModule.FirstClass = FirstClass;      var SecondClass = (function () {         function SecondClass() {         }         SecondClass.prototype.showSomething = function () {             alert(x.toString());         };         return SecondClass;     })();     MyModule.SecondClass = SecondClass; })(MyModule || (MyModule = {}));  var a = new MyModule.FirstClass(); a.doSomething();  var b = new MyModule.SecondClass(); b.showSomething(); 
like image 36
Fenton Avatar answered Oct 07 '22 18:10

Fenton