TypeScript 0.8.2在内部模块中导入Node.js模块

好的,我可以看到你想在你的项目中使用内部模块。 那么,在TypeScript 0.8.1.1中有一个解决方法,你可以定义非导出模块(内部),并在其上面添加导入。 在0.8.2中,似乎这不起作用了。 我在这里看到的选项是完全省略导入语法,并使用节点模块的标准要求。 我不知道这是不是一个好主意,但请分享你的意见。 我知道使用导入语法将使模块外部(语言规范),但在0.8.1.1,这不是真的,错误也许?

在TypeScript 0.8.1.1中,这个工作并且在0.8.2中不起作用:

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

我可以在其他内部模块的文件顶部使用引用语法来引用包含上面代码的文件,并且通常会调用:

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

看来在0.8.2中,这是内部模块工作的唯一方式:

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

有没有其他的可能性混合内部模块与Node.js模块? 上面有没有什么需要使用(它编译和运行没问题…)?

我认为TypeScript 0.8.2使我们更接近规范。

语法:

 import x = module('SomeModule'); 

特别是TypeScript语言规范中的ExternalModuleReference

内部模块将被导入使用:

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

但是导入一个内部模块不会在JavaScript中产生一个require语句。

取自TypeScript语言规范0.8 – 9.2.2导入声明

导入声明:

 import Identifier = ModuleReference ; 

ModuleReference:

 ExternalModuleReference ModuleName 

ExternalModuleReference:

 module ( StringLiteral ) 

好的,这个错误是由于TypeScript的版本。 在TypeScript 0.8.1.1中导入一个外部模块的语法必须是:

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

这是在最新版本的TypeScript中发现的错误,您可以返回到以前的版本或更改语法以使其与v0.8.1.1兼容。 请记住,这是一个错误,在未来的版本中,您应该能够使用原始语法。

这是这个bug的官方线程: http : //typescript.codeplex.com/discussions/405800