Consolidating classes into common namespace yields inaccessible module errors

Topics: General
Sep 20, 2013 at 3:11 PM
In my project, I have a bunch of classes in separate files that I'd like to reference from a common namespace. Here's an example of what I'm trying to do:

widget1.ts (widget2.ts, widget3.ts...)
export class Widget1 {}
allWidgets.ts
import w1 = require('widget1')
import w2 = require('widget2')
// import all other widgets...

export var Widget1 = w1.Widget1;
export var Widget2 = w2.Widget2;
// export all other widgets...
client.ts
import allWidgets = require('allWidgets');

export function doWork() {
    var w1 = allWidgets.Widget1();
}
So far this works fine. I can reference all the widget classes through allWidgets. The issue I'm running into occurs when I attempt to either return a widget or pass a widget as a parameter to a function:

clientWithErrors.ts
import allWidgets = require('allWidgets');

export function doWork() {
    // error TS2067: Return type of exported function is using inaccessible module
    return new allWidgets.Widget1();
}

// error TS2049: Parameter 'widget' of exported function is using inaccessible module
export function doOtherWork(widget: typeof allWidgets.Widget1) {}
I can get around this by importing a Widget1 directly into the client file, even if I don't use it:

clientWorkaround.ts
import allWidgets = require('allWidgets');
import _Widget1 = require('widget1');

export function doWork() {
    return new allWidgets.Widget1();
}

export function doOtherWork(widget: typeof allWidgets.Widget1) {}
The direct import seems to only be necessary to make the TypeScript compiler happy, since no reference to the direct import appears in the generated JavaScript.

clientWorkaround.js
var allWidgets = require("./allWidgets");


function doWork() {
    return new allWidgets.Widget1();
}
exports.doWork = doWork;

function doOtherWork(widget) {
}
exports.doOtherWork = doOtherWork;
I can't tell if this is a TypeScript bug or if there's a better way to structure the code in this case. Or maybe I just need to accept that I need to import everything directly and move on.
Sep 20, 2013 at 3:15 PM
+1
Developer
Sep 20, 2013 at 6:22 PM
I think this will fix things:
export import allWidgets = require('allWidgets');
Sep 20, 2013 at 6:27 PM
@danquirk, I'm still getting the inaccessible module errors with the code you suggested above
Developer
Sep 21, 2013 at 3:05 PM
@jpmoodlerooms You're right, that is a bug.
Sep 23, 2013 at 8:14 AM
Might this be the same bug as what I reported?

https://typescript.codeplex.com/discussions/456757
Sep 23, 2013 at 1:26 PM
It's the same problem as reported in this issue: https://typescript.codeplex.com/workitem/1647