下面是.js文件:
var mongoose=require('mongoose');
var express=require('express');
var app=express();
var connect=mongoose.connect("mongodb://localhost:27017/techo2log");
var schema=mongoose.Schema;
var userschema=new schema(
{
name:String,
age:Number,
address:String,
created_at:Date,
updated_at:Date,
}
);
var User=mongoose.model('User',userschema);
userschema.method.dudify=function()
{
this.name=this.name+'-dude';
return this.name;
}
//making the model available to the external file
module.exports=User;如果我为上面的代码编写typescript定义文件,以便可以在我的.ts文件中导入,我想在我的类型脚本file.How中使用用户模块。
发布于 2016-07-29 22:40:01
我假设您编写了一个NodeJS模块,所以首先,确保您使用--module commonjs作为编译器选项(或在您的tsconfig.json中)。
其次,您只需要创建一个定义文件。如果您的JS文件名为'user.js',您可以将您的定义文件命名为'user.d.ts‘,如下所示:
declare module 'user' {
var User: any;
export = User;
}当然,您可以改进User的类型,但作为开始,这应该能起到作用。
第三,您可以在TypeScript中使用这样一行代码导入您的模块:
import User = require('user');https://stackoverflow.com/questions/38659143
复制相似问题