-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Typescript schema doesn't have _id field. #14286
Comments
import mongoose, { type InferSchemaType, Schema } from "mongoose";
const eventSchema = new mongoose.Schema({ url: String }, { timestamps: true });
const Event = mongoose.model("Event", eventSchema);
type EventDoc = InferSchemaType<typeof eventSchema>;
async function run() {
const event = {} as EventDoc;
// createdAt exists because timestamps: true, which is totally OK
event.createdAt = new Date();
// WRONG: _id does not exist on type EventDoc, this should be set, since options._id is to true by default
event._id; // ERROR
}
run(); |
I agree this is a bit confusing, however currently the issue is that type EventDoc = ReturnType<(typeof Event)['hydrate']>; //InferSchemaType<typeof eventSchema>; |
I think we'll have to make some docs changes to account for this, adding |
docs: de-emphasize `InferSchemaType<>` in TypeScript docs in favor of automatic inference
Prerequisites
Mongoose version
8.0.4
Node.js version
16
MongoDB server version
6.0.6
Typescript version (if applicable)
5.2.2
Description
Hello,
I think the
SchemaType
of a schema should have an_id
field by defaultFor e.g, we have
createdAt
, andupdatedAt
field in the type when we set theoption { timestamps: true }
.Which is not the case when we set
options: { _id: true }
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: