I'm using TypeScript and require.js to resolve dependencies in my files. I'm in a situation where I want to reference a static field of a class in an other class, but in the same internal module (same folder) and I am not able to access it in that specific context. Also, the pre-compiler doesn't show any error in my code, but when accessing the static field at execution, I only get an undefined
value.
Here are the relevant files :
Game.ts containing the variable to be accessed
import Scene = require("Scene");
import Stage = require("Stage");
class Game {
// ...
static GAME_WIDTH: number = 1920;
// ...
public start(): void {
var currentScene: Scene = new Stage(); // Calls the Stage constructor
}
}
export = Game;
Launcher.ts entry point of the execution
import Game = require("Game");
console.log(Game.GAME_WIDTH); // Correctly shows "1920"
var onlyGame: Game = new Game();
onlyGame.start();
Stage.ts
in which I'm trying to refer to Game.GAME_WIDTH
import Game = require("Game");
import Scene = require("Scene");
class Stage implements Scene {
constructor() {
console.log(Game.GAME_WIDTH); // Shows "undefined"
}
}
export = Stage;
Launcher.ts
will execute first, creating a Game
instance and starting it, which will create a Stage
instance that seem to not be able to access Game.GAME_WIDTH
anymore. I don't understand why I can access the static variable once but can't do it again later in the execution. And the Visual Studio pre-compiler sees nothing wrong in my code.
Can somebody help me with what's wrong here ?
Thanks !