-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Open
Labels
typescriptTypes or Types-test related issue / Pull RequestTypes or Types-test related issue / Pull Request
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
9.2.2
Node.js version
24.13.1
MongoDB server version
n/a
Typescript version (if applicable)
5.8.3
Description
calling toObject after populate does not keep the populated type in the plain object type
Steps to Reproduce
const childSchema = new Schema({
name: String
});
const Child = mongoose.model('Child', childSchema);
const parentSchema = new Schema({
title: String,
children: [{ type: Schema.Types.ObjectId, ref: 'Child' }]
});
const Parent = mongoose.model('Parent', parentSchema);
async function reproduce() {
const parentDoc = new Parent({ title: 'My Parent', children: [] });
const populatedDoc = await parentDoc.populate<{
children: Types.DocumentArray<HydratedDocFromModel<typeof Child>>;
}>('children');
const childNameFromHydrated = populatedDoc.children[0]?.name; // OK
const plainObject = populatedDoc.toObject();
const childNameFromPlain = plainObject.children[0].name; // Property 'name' does not exist on type 'ObjectId'. ts(2339)
}Expected Behavior
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
typescriptTypes or Types-test related issue / Pull RequestTypes or Types-test related issue / Pull Request