最佳答案
试图在 Typecript 中实现一个 Mongoose 模型。搜索谷歌只发现了一种混合的方法(结合了 JS 和 TS)。如果没有 JS,如何按照我相当天真的方法实现 User 类?
希望能够在没有包袱的情况下使用 IUserModel。
import {IUser} from './user.ts';
import {Document, Schema, Model} from 'mongoose';
// mixing in a couple of interfaces
interface IUserDocument extends IUser, Document {}
// mongoose, why oh why '[String]'
// TODO: investigate out why mongoose needs its own data types
let userSchema: Schema = new Schema({
userName : String,
password : String,
firstName : String,
lastName : String,
email : String,
activated : Boolean,
roles : [String]
});
// interface we want to code to?
export interface IUserModel extends Model<IUserDocument> {/* any custom methods here */}
// stumped here
export class User {
constructor() {}
}