Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript 0.8.2 importing Node.js modules in internal modules

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 ...)?

like image 317
jzvelc Avatar asked Jan 23 '13 12:01

jzvelc


People also ask

Does TypeScript use CommonJS?

TypeScript supports the following 4 Modules: commonjs, amd, system and umd.

What is internal module in TypeScript?

Internal Module Internal modules were in the earlier version of Typescript. It was used for logical grouping of the classes, interfaces, functions, variables into a single unit and can be exported in another module. The modules are named as a namespace in the latest version of TypeScript.


1 Answers

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 )
like image 169
Fenton Avatar answered Oct 30 '22 23:10

Fenton