-
-
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 errors with .lean() after upgrading from Mongoose 8.7.1 to 8.7.2 #14974
Open
2 tasks done
Labels
typescript
Types or Types-test related issue / Pull Request
Comments
EDIT: I've opened a separate issue to avoid polluting this one |
Seeing the same thing, probably related to #14902 and #14967 Here is a fully reproducible example which worked in 8.7.1: import { Schema, model } from 'mongoose';
import type { Document, ObjectId } from 'mongoose';
export interface Foo {
_id: ObjectId;
bar: string;
}
export type FooDocument = Foo & Document<ObjectId>;
const schema = new Schema<Foo>(
{
bar: { type: String, required: true },
},
{ collection: 'foo', timestamps: true },
);
export const FooModel = model<Foo>('foo', schema);
const test: FooDocument[] = await FooModel.find({ bar: 'asd' }).lean();
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
8.7.2
Node.js version
20.15.1
MongoDB server version
7.0.14
Typescript version (if applicable)
5.6.3
Description
Hi Mongoose team,
After upgrading Mongoose from version 8.7.1 to 8.7.2, I'm encountering TypeScript errors when running queries that use the .lean() method. The issue seems to be related to changes in the type utility chain, specifically in how FlattenMaps and BufferToBinary are applied.
Here’s the TypeScript error:
Code that previously ran without issues, such as:
or
is now throwing the above TypeScript error.
Previously, the type chain in mongoose\types\query.d.ts was:
Require_id<FlattenMaps<BufferToBinary<RawDocType>>>
in the code:
But after the update, the order of FlattenMaps and BufferToBinary has been swapped to:
Require_id<BufferToBinary<FlattenMaps<RawDocType>>>
like:
This change appears to have introduced stricter typing or structural mismatches in queries that return lean documents, particularly when using .lean() or .lean() without explicit typing. The flipping of FlattenMaps and BufferToBinary seems to cause issues when dealing with Map or Buffer fields in the schema. I think this is breaking change, and should be reverted.
Steps to Reproduce
or
This will run without issues.
Upgrade to Mongoose 8.7.2
Run the same queries, and then TypeScript will show an error.
Expected Behavior
Expected Behavior:
Queries using .lean() should return plain objects that conform to the expected TypeScript interface (e.g., Blog[] in the case of a Blog model) without any TypeScript errors, as it worked in version 8.7.1.
As a workaround, I’ve had to explicitly type the result of .lean(), but this was not required in previous versions:
The text was updated successfully, but these errors were encountered: