Skip to content

types(model): use ProjectionType for Model.hydrate() #15443

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

Merged
merged 1 commit into from
May 30, 2025
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
23 changes: 23 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,26 @@ async function gh15369() {
throw error;
}
}

async function gh15437() {
interface Person {
name: string;
age: number;
address: string;
}

const schema = new mongoose.Schema({
name: String,
age: Number,
address: String
});
const PersonModel = model<Person>('Person', schema);

const data = { name: 'John Doe', age: 30, address: '123 Main St' };

// Test hydrating with string projection
const doc1 = PersonModel.hydrate(data, 'name age');
expectType<string>(doc1.name);
expectType<number>(doc1.age);
expectAssignable<undefined | null | string>(doc1.address);
}
2 changes: 1 addition & 1 deletion types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ declare module 'mongoose' {
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
* The document returned has no paths marked as modified initially.
*/
hydrate(obj: any, projection?: AnyObject, options?: HydrateOptions): THydratedDocumentType;
hydrate(obj: any, projection?: ProjectionType<TRawDocType>, options?: HydrateOptions): THydratedDocumentType;

/**
* This function is responsible for building [indexes](https://www.mongodb.com/docs/manual/indexes/),
Expand Down