-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Closed
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.1.3
Node.js version
24.11.0
MongoDB server version
na
Typescript version (if applicable)
5.9.3
Description
It seems like using timestamps: true and defining virtuals on a schema causes some fields in the toObject type to incorrectly be {} | undefined.
When I remove timestamps: true OR remove the virtuals definition, the toObject types are correct.
Steps to Reproduce
import mongoose, { Schema } from 'mongoose';
const FooSchema = new Schema({
foo: { type: String },
});
const PodcastSchema = new Schema({
a: { type: FooSchema },
b: { type: FooSchema, required: true },
c: { type: String },
d: { type: String, required: true },
}, {
timestamps: true,
virtuals: { hello: { get() { return 'hello world' } } },
});
const RootModel = mongoose.model('Root', PodcastSchema);
async function example() {
const roots = await RootModel.find();
const root = roots[0].toObject({ flattenMaps: true, flattenObjectIds: true, versionKey: false, virtuals: true });
// const root: {
// id: string;
// a: {} | undefined;
// b: { ... 1 more } | null | undefined;
// c: {} | undefined;
// d: {} | undefined;
// hello: string;
// createdAt: NativeDate;
// updatedAt: NativeDate;
// }
}Expected Behavior
The toObject type should be:
const root: {
id: string;
a: { ... 1 more } | null | undefined;
b: { ... 1 more };
c: string | null | undefined;
d: string;
hello: string;
createdAt: NativeDate;
updatedAt: NativeDate;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
typescriptTypes or Types-test related issue / Pull RequestTypes or Types-test related issue / Pull Request