Skip to content

Commit d7d56dd

Browse files
committed
types(model): use ProjectionType for Model.hydrate()
Fix #15437
1 parent 0c5f56f commit d7d56dd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

test/types/models.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,3 +1010,26 @@ async function gh15369() {
10101010
throw error;
10111011
}
10121012
}
1013+
1014+
async function gh15437() {
1015+
interface Person {
1016+
name: string;
1017+
age: number;
1018+
address: string;
1019+
}
1020+
1021+
const schema = new mongoose.Schema({
1022+
name: String,
1023+
age: Number,
1024+
address: String
1025+
});
1026+
const PersonModel = model<Person>('Person', schema);
1027+
1028+
const data = { name: 'John Doe', age: 30, address: '123 Main St' };
1029+
1030+
// Test hydrating with string projection
1031+
const doc1 = PersonModel.hydrate(data, 'name age');
1032+
expectType<string>(doc1.name);
1033+
expectType<number>(doc1.age);
1034+
expectAssignable<undefined | null | string>(doc1.address);
1035+
}

types/models.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ declare module 'mongoose' {
482482
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
483483
* The document returned has no paths marked as modified initially.
484484
*/
485-
hydrate(obj: any, projection?: AnyObject, options?: HydrateOptions): THydratedDocumentType;
485+
hydrate(obj: any, projection?: ProjectionType<TRawDocType>, options?: HydrateOptions): THydratedDocumentType;
486486

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

0 commit comments

Comments
 (0)