Closed
Description
Prerequisites
- I have written a descriptive issue title
Mongoose version
6.4.0
Node.js version
18.3
MongoDB version
Unsure
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
arch-5.18.5 something
Issue
Using InferSchemaType
to create the schema interface type, how do I access the _id
field? If I put it in the schema definition, I have to also set it explicitly when inserting new documents. If I don't put it there, InferSchemaType
does not include it in the interface.
What do I do? I'd like to know the same for .save()
and so on. Am I supposed to do a type union?
Code:
import { Document, InferSchemaType, Schema } from "mongoose"
const mySchema = new Schema({
foo: { type: String, required: true }
})
type MySchemaType_Problematic = InferSchemaType<typeof mySchema>
// MySchemaType_Problematic is missing the _id field along with the save() method etc.
// Workaround:
type MySchemaType_Workaround = InferSchemaType<typeof mySchema> & Document
// MySchemaType_Workaround can now be used properly, with _id and save() etc.