Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript - Module is undefined at runtime

I dont understand what i'm doing wrong. I've create a TypeScript project in VS2012, and created a file named "Vector.ts", in a sub-directory named "Physics":

// Module
module Physics {

    // Class
    export class Vector {
        constructor(public x: number) { }
    }

}

Also, I have the following app.ts file:

/// <reference path="Physics/Vector.ts" />

window.onload = () => {
    var vector = new Physics.Vector(6);
};

The project is successfully compiled, but when i launch it, i get the following exception:

0x800a1391 - JavaScript runtime error: 'Physics' is undefined

I don't understand what am i doing wrong...

Thanks.

like image 412
gipouf Avatar asked Mar 16 '13 16:03

gipouf


1 Answers

If you are using AMD style modules with a module loader such as Require.Js, you need an import statement. See Steve's accepted answer here: https://stackoverflow.com/a/14919495/1014822 Your code will look something like:

import Physics = module("Physics/Vector");

var vector = new Physics.Vector(6);

... and you won't need this: /// <reference path="Physics/Vector.ts" />

If you are not using a module loader, you just need to make sure you include your Vector.js output file somewhere in your html page, and make sure it loads before your app file. In this case, you use /// <reference path="Physics/Vector.ts" /> to tell TS where to find your module for intellisense etc. to work.

like image 114
Jude Fisher Avatar answered Oct 16 '22 04:10

Jude Fisher