I have two files
app.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
}
}
}
So question is, what am I missing? How should i go about importing mongodb?
It looks like you might be confusing external modules (export
/ import
) with the "internal" module
block? Import declarations should only be present at the top level of a file.
I think the problem is that TS reference paths are includes, not imports. As long as you do not use import/export, the TS compiler puts everything in the 'global module', using reference paths to stitch it together.
Once you start using import/export in a file, that file will be interpreted as its own module, no longer part of the global module. Have a look into the TS language specification, section 9.1, for the details.
So, I suspect that -once you start using import in MyDataBase.ts
- you'll need to import that file as an external module, too (instead of just referencing it).