TS, RequireJS, and internal module refs

Topics: General
May 9, 2014 at 4:21 PM
Edited May 13, 2014 at 9:18 AM
I think this is mostly an issue with my misunderstanding with how AMD modules are used by TS, so hopefully someone can fill the gaps quickly.

I've just introduced RequireJS into our project in an effort to simplify our script loading. I have the following TS class:
declare module MyApp {
    export class Builder {
        constructor() {
        }
    }
}
I can obviously reference this class from elsewhere by calling new MyApp.Builder() directly.

I then decided to introduce an external module dependency as follows:
import myDep = require('myDep');

module MyApp {
    export class Builder {
        constructor() {
            myDep.doStuff();
        }
    }
}
However, the issue now is that any existing calls to new MyApp.Builder() don't work - the compiler complains that Builder doesn't exist on MyApp. Presumably because my class is now defined as an AMD module in the resulting JS?

This seems to be telling me that use of RequireJS is an all-or-nothing proposition with TS - that is, if I stick with v2 above, I'd need to export MyApp.Builder and import it using require into any classes needing to reference it. Is this the case?
May 12, 2014 at 10:36 AM
Why have you written the declare keyword in front of module? Declarations can't contain implementation.
May 13, 2014 at 9:17 AM
Edited May 13, 2014 at 9:18 AM
Ignore that, was a just a typo in my example. Have corrected.
May 13, 2014 at 4:30 PM
I'll let others opine on the technical reasons, but yes - in my experience moving to external modules is a big "all or nothing" production that has many ripple effects with TypeScript 1.0. I've heard that the team is thinking about how to make this easier, but today it's quite hard and you really have to be sure that you'll be get benefits from it versus internal modules which are quite easy to code with.
Marked as answer by MarcusWhit on 5/14/2014 at 1:27 AM
Developer
May 13, 2014 at 7:50 PM
MarcusWhit wrote:
This seems to be telling me that use of RequireJS is an all-or-nothing proposition with TS - that is, if I stick with v2 above, I'd need to export MyApp.Builder and import it using require into any classes needing to reference it. Is this the case?
In short, yes. Mixing internal and external modules is not really going to work. The reasons for that are a little more obvious when you look at the JS codegen.
Marked as answer by MarcusWhit on 5/14/2014 at 1:27 AM