LeanDocument Type doesn't include _id or id
Problem
*Do you want to request a feature or report a bug? Report a bug What is the current behavior? I believe that the types defined here are incorrect: https://github.com/Automattic/mongoose/blob/c22152c5a95c5ce9ed688d96b37b05c7df6be1ec/types/index.d.ts#L2261-L2267 This says that `LeanDocuments` (that is documents returned from `.toObject()` or when using the `lean` option don't have the `_id` or `id` fields. I don't think this is correct. This issue only presented itself in version 6.3.2 and wasn't present in 6.3.1 If the current behavior is a bug, please provide the steps to reproduce. [code block] What is the expected behavior? `_id` and `id` are present on LeanDocuments (I think...) What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.* Node.js: v16.15.0 TypeScript: v4.6.2 mongoose: v6.3.2
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix LeanDocument Type to Include _id and id Fields
In version 6.3.2 of Mongoose, the TypeScript definitions for LeanDocuments incorrectly omit the _id and id fields. This change was not present in version 6.3.1, leading to confusion and potential errors in TypeScript applications that expect these fields to be included.
Awaiting Verification
Be the first to verify this fix
- 1
Identify the TypeScript Definitions File
Locate the TypeScript definitions file for Mongoose where LeanDocument types are defined. This can be found in the Mongoose GitHub repository under types/index.d.ts.
yamlhttps://github.com/Automattic/mongoose/blob/c22152c5a95c5ce9ed688d96b37b05c7df6be1ec/types/index.d.ts - 2
Modify LeanDocument Type Definitions
Edit the LeanDocument type definition to include the _id and id fields. This can be done by creating a custom type definition file in your project that extends the existing Mongoose types.
typescriptdeclare module 'mongoose' { interface LeanDocument<T> { _id: string; id: string; } } - 3
Test the Changes
Run your TypeScript application and ensure that LeanDocuments now correctly include the _id and id fields. You can create a simple test case that retrieves a document using the lean option and checks for the presence of these fields.
typescriptconst doc = await Model.findOne().lean(); console.log(doc._id, doc.id); // Should log the _id and id values - 4
Update TypeScript Configuration
Ensure that your TypeScript configuration is set to recognize the custom type definitions. This might involve adding the path to your custom type definitions in the tsconfig.json file.
json{ "compilerOptions": { "typeRoots": ["./node_modules/@types", "./path/to/custom/types"] } } - 5
Document the Changes
Update your project documentation to reflect the changes made to the LeanDocument type definitions. This will help other developers understand the modifications and ensure consistency across the codebase.
markdownUpdate README.md with information about LeanDocument type changes.
Validation
Confirm that LeanDocuments returned from Mongoose queries now include the _id and id fields by running the test case and checking the output. Additionally, ensure that TypeScript does not throw any errors related to these fields.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep