Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(model+query): pass TInstanceMethods to QueryWithHelpers so populated docs have methods #14581

Merged
merged 1 commit into from
May 9, 2024
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