Search code examples
javascripttypescriptamddurandal

Comment referenced class throws ReferenceError at runtime


This builds with no errors at compile time, but when I run it, I'm getting a ReferenceError when I try to use the net class. (net is not defined)

//chat.ts file
/// <reference path="../client/net.ts" />
class chat {
    constructor(public chatHub: any) {
       net.call(chatHub.server.getUsers());
    } 
}

//net.ts file
class net {
    public call(callback: () => any): any {
        //stuff here
        return callback();
    }
} 

Things I tried:

  • Turn net into a module and export the function. Won't work because doing an export triggers the AMD module transformation, which then requires Importing it in my chat class, which turns my chat class into an AMD module rendering it out of scope for all the classes that reference chat.ts. Cascade of AMD doom.
  • Tried all possible permutations of static, public, module, class and export. No dice.

All I want to do is use the utility functions on my net class from my chat class.


Solution

  • Did you remember to add the net.js file to the page (somewhere above the chat.js file)? Just referencing it in the TypeScript doesn't load it for you.

    You either need to use the AMD module definition in conjunction with a loader like require.js, or make sure all the .js files you need are added (either one at a time, or bundled) to your page. All the comment reference does is tell the compiler where to find the type information.