TypeScript: cannot refer to static field in specific context

I am using TypeScript and require.js to resolve dependencies in my files. I'm in a situation where I want to refer to a static field of a class in a different class, but in the same inner module (in the same folder), and I can't access it in that specific context. Also, the precompiler doesn't detect any errors in my code, but when accessing a static field at runtime, I only get the value undefined

.

Here are the relevant files:

Game.ts containing an accessible variable

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 to execute

import Game = require("Game");

console.log(Game.GAME_WIDTH);    // Correctly shows "1920"
var onlyGame: Game = new Game();
onlyGame.start();                

      

Stage.ts in which I am trying to refer toGame.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 by instantiating Game

and starting it, creating an instance Stage

that it can't seem to access anymore Game.GAME_WIDTH

. I don't understand why I can access the static variable once, but I cannot do so later in the run. And the Visual Studio precompiler doesn't see anything in my code.

Can someone help me with what is wrong here?

Thank!

+3


source to share





All Articles