Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,44 @@ function gh14441() {
expectType<string>(docObject.child.name);
});
}

async function gh14574() {
// Document definition
interface User {
firstName: string;
lastName: string;
friend?: Types.ObjectId;
}

interface UserMethods {
fullName(): string;
}

type UserModelType = mongoose.Model<User, {}, UserMethods>;

const userSchema = new Schema<User, UserModelType, UserMethods>(
{
firstName: String,
lastName: String,
friend: { type: Schema.Types.ObjectId, ref: 'User' }
},
{
methods: {
fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
}
);
const userModel = model<User, UserModelType>('User', userSchema);

const UserModel = () => userModel;

const user = await UserModel()
.findOne({ firstName: 'b' })
.populate<{ friend: HydratedDocument<User, UserMethods> }>('friend')
.orFail()
.exec();
expectType<string>(user.fullName());
expectType<string>(user.friend.fullName());
}
Loading