5

I have a class which looks like this:

export module GameModule {
    export class Game {
        private boardContainer: HTMLElement;
        private board: number[][];

        constructor (container: HTMLDivElement) {
            this.boardContainer = container;
            this.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

            this.drawGrid();
        }

        drawGrid() {

        }
    }
}

and the main application:

import GameModule = module("./GameModule");

window.onload = () => {
    new GameModule.GameModule.Game(<HTMLDivElement> document.getElementById('content'));
};

But when I'm trying to compile the code, I'm getting errors:

>tsc --module amd app.tss
app.tss(1, 27): The name '"./GameModule"' does not exist in the current scope
app.tss(1, 27): A module cannot be aliased to a non-module type
app.tss(4, 8): Expected car, class, interface, or module

What's wrong with my code?

skayred
  • 10,603
  • 10
  • 52
  • 94
  • Hi @skayred! what is the name of the first file that you are trying to load the module? The name of first file must be "GameModule.ts." Can you verify this? – Diullei Oct 16 '12 at 11:45

1 Answers1

7

Your code compiles fine for me. Here is what I have:

GameModule.ts

export module GameModule {
    export class Game {
        private boardContainer: HTMLElement;
        private board: number[][];

        constructor (container: HTMLDivElement) {
            this.boardContainer = container;
            this.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

            this.drawGrid();
        }

        drawGrid() {

        }
    }
}

Main.ts

import GameModule = module("./GameModule");

window.onload = () => {
    new GameModule.GameModule.Game(<HTMLDivElement> document.getElementById('content'));
};

both in the same directory. When I compile it with

tsc --module amd Main.ts

the compiler reports no problems. Can you please check if you can compile exactly those two classes? Also, please post your complete code (if possible). From the error message I would assume that your module resides in another directory.

Valentin
  • 7,874
  • 5
  • 33
  • 38
  • My main class was names `app.ts`, and GameModule was in `GameModule.ts`. Was it issue in my naming? – skayred Oct 16 '12 at 12:02
  • @skayred I just noticed that you compiled app.tss . Was that a typo in your question? `GameModule.ts` is the correct name. You are using external modules, so the module import needs the relative name of the file that contains the module. – Valentin Oct 16 '12 at 12:06
  • I've reproduce this bug again. Just create new VS2012 TypeScript project, change `app.js` content to my and create `GameModule.ts`. When you try to compile it, you get an error – skayred Oct 17 '12 at 02:49
  • Is it necessary to type GameModule twice? (As in new GameModule.GameModule.Game...) – stepancheg Jun 24 '13 at 22:27
  • @stepancheg It used to be. I'm unsure if that's still true with the latest version of the compiler but I guess so. – Valentin Jun 25 '13 at 07:38