Skip to content

Typescript schema doesn't have _id field. #14286

@abarriel

Description

@abarriel

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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 default

For e.g, we have createdAt, and updatedAt field in the type when we set the option { timestamps: true }.

Which is not the case when we set options: { _id: true }

Steps to Reproduce

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();

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions