1

我有两个文件

应用程序.js

///<reference path='mongodb.d.ts'/>
///<reference path='MyDatabase.ts'/>
module MyModule {
  import mongodb = module("mongodb");
  new mongodb.Server();
  var db = new MyDatabase(); // this will not work with first import line in Database.js, but work with second
}

MyDatabase.js

///<reference path='mongodb.d.ts'/>

import mongodb = module("mongodb"); // adding this line here, will cause that app.js will not see MyDatabase class

module MyModule {
   import mongodb = module("mongodb"); // adding this line this will cause that classes in this module cant use mongodb

   export class MyData {
      _id: mongodb.Id; // second import line will cause this to be compilation error, with first line it works
   }

   export class MyDatabase {
       public test(): void {
          //with second line i can access mongodb here
       }
   }
}

所以问题是,我错过了什么?我应该如何导入 mongodb?

4

2 回答 2

1

看起来您可能将外部模块 ( export/ import) 与“内部”module块混淆了?导入声明应该只出现在文件的顶层。

于 2012-11-27T20:29:06.903 回答
0

我认为问题在于 TS 引用路径是包含,而不是导入。只要您不使用导入/导出,TS 编译器就会将所有内容都放在“全局模块”中,并使用参考路径将它们拼接在一起。

一旦您开始在文件中使用导入/导出,该文件将被解释为它自己的模块,不再是全局模块的一部分。查看 TS 语言规范,第 9.1 节,了解详细信息。

所以,我怀疑——一旦你开始使用 import in——你MyDataBase.ts也需要将该文件作为外部模块导入(而不仅仅是引用它)。

于 2012-11-28T18:11:14.997 回答