TypeScript 0.8.2 importing Node.js modules in internal modules

Problem

Okay, as I can see you would like to use internal modules in your project. Well, there was a workaround in TypeScript 0.8.1.1, you could define non exported module (internal) and add imports above it. In 0.8.2 it seems that this doesn't work anymore. Only option I see here would be to completely omit import syntax and use standard require for node modules. I don't know if this is a good idea but please, share your opinions. I know that using import syntax will make module external (language specification), but that wasn't true in 0.8.1.1, bug maybe?

In TypeScript 0.8.1.1 this worked and doesn't work in 0.8.2 anymore:

import path = module('path');
import fs = module('fs');
module SomeNamespace.Controller {
    export class Index {
        ...
    }
}

I could reference file including above code using reference syntax on top of file in other internal modules and normally call:

var ctrl = new SomeNamespace.Controller.Index;
ctrl.index();

It seems that in 0.8.2 this is the only way what it works for internal modules:

var path = require('path');
var fs = require('fs');
module SomeNamespace.Controller {
    export class Index {
        ...
    }
}

Are there any other possibilities to mix internal modules with Node.js modules? Is there something wrong with above require usage (it compiles and runs okay ...)?

Problem courtesy of: jzvelc

Solution

I think that TypeScript 0.8.2 takes us closer to the specification.

The syntax:

import x = module('SomeModule');

Is specifically an ExternalModuleReference in the TypeScript Language Specification.

An internal module would be imported using:

///<reference path="SomeModule.ts" />
import x = SomeModule;

But importing an internal module won't generate you a require statement in your JavaScript.

Taken from TypeScript Language Specification 0.8 - 9.2.2 Import Declarations

ImportDeclaration:

import Identifier = ModuleReference ;

ModuleReference:

ExternalModuleReference
ModuleName

ExternalModuleReference:

module ( StringLiteral )
Solution courtesy of: Fenton

Discussion

Ok, this error is due to the version of the TypeScript. In TypeScript 0.8.1.1 to import an external module the syntax has to be:

export import <moduleName> = module(“<path>”);

This is a bug identified in the latest version of TypeScript, you can return to the previous version or change the syntax to make it compatible with v0.8.1.1. Have in mind that this is a bug and in future versions, you should be able to use the original syntax.

This is the official thread for this bug: http://typescript.codeplex.com/discussions/405800

Discussion courtesy of: unjuken

This recipe can be found in it's original form on Stack Over Flow.