在我自己的定义文件中使用mongodb类型定义

时间:2016-03-06 11:36:39

标签: mongodb import typescript

我遇到TypeScript导入问题:

这是我的 mydef.d.ts

import * as mongodb from "mongodb";
interface UserDto {
    _id: mongodb.ObjectID;
    username: string;
}

这是我的 main.ts

import * as mongodb from "mongodb";
let user: UserDto = {
    _id: new mongodb.ObjectID("anyID"),
    username: "Xstoudi"
}

但是在main.ts中:Cannot find name UserDto

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,您必须 mydef.d.ts 导出您的界面。

import * as mongodb from "mongodb";
export interface UserDto {
    _id: mongodb.ObjectID;
    username: string;
}

然后您需要 main.ts 导入,就像导入mongo一样。

import * as mongodb from "mongodb";
import {UserDto} from "mydef";
let user: UserDto = {
    _id: new mongodb.ObjectID("anyID"),
    username: "Xstoudi"
}