interface User {
_id: GooseTypes.ObjectId;
email: string;
}
interface UserDoc extends User, Document {
_id: GooseTypes.ObjectId;
}
const UserSchema = new Schema({
email: String,
});
const UserModel = model<UserDoc>("User", UserSchema);
const userDocument = new UserModel({email: "test@test.com"}); //returns UserDoc type
const userObject = userDocument.toObject(); // returns any, have to manually cast to User
目前,我正在使用@ types / mongoose。我想避免以User身份执行toObject(),因为这会导致出现错误的地方。
如何扩展toObject()方法以返回类型为User / OtherType的对象?