TS Error specifically with `lean` result
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.3.5
Node.js version
18.2.0
MongoDB server version
4.5.0
Description
Firstly, Thanks for the amount of effort you put into this package.
The way the new lean type is computed is very frustrating to use. With the object keys it omits from the type and repeated computed names, it makes it hard to understand what the actual result type will be by just hovering over the variable.
I upgraded from v6.3.1
to v6.3.5
and the changes broke the lean result type in my application. The ObjectId
type I use is defined with the one in bson
as well.
import { ObjectId } from "bson";
import { Schema } from "mongoose";
type ObjectId = ObjectId & Schema.Types.ObjectId;
I do it this way to take advantage of the bson
type definition for an ObjectId
. Like with the .equals
method to test the equality of an ObjectIdLike
variable with the ObjectId. TS reported the property doesn’t exist in the lean return type, but I tested and it did.
I edited the definition of your types for the LeanResult to something more straightforward, the error was resolved and it was easier to glance at the type when I hovered over the variable.
// https://github.com/Automattic/mongoose/blob/9875dbf53be7b1a4652ab3ac57db2a881999889a/types/index.d.ts#L529
export type LeanDocument<T> = {
[K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>]: T[K] extends never[] ? never[] : T[K] extends Document[] ? Array<LeanDocument<T[K][number]>> : T[K] extends unknown[] ? T[K][number] extends Document ? Array<LeanDocument<T[K][number]>> : T[K] : T[K] extends Document ? LeanDocument<T[K]> : T[K];
};
// https://github.com/Automattic/mongoose/blob/9875dbf53be7b1a4652ab3ac57db2a881999889a/types/query.d.ts#L422
lean<LeanResultType = ResultType extends unknown[] ? Array<LeanDocument<Require_id<RawDocType>>> : LeanDocument<Require_id<RawDocType>> | null>(val?: boolean | any): QueryWithHelpers<LeanResultType, DocType, THelpers, RawDocType>;
I’ve tested it with your tests defined in https://github.com/Automattic/mongoose/blob/9875dbf53be7b1a4652ab3ac57db2a881999889a/test/types/lean.test.ts and it works as well.
This is what it looks like with the current LeanDocument
definition
Notice the 52+ properties omitted from the result
Also, In the definition for the Require_id
After checking if it extends a _id
, doing this (T & { _id: T['_id'] })
again rather than just using the T
type since it already has the _id
property only causes it to look more unorganised in the hover tooltip.
Using (T & { _id: T['_id'] })
Using T
Please let me know what you think about this.
If there is something you’ll like to change or a reason why you wouldn’t want this change.
Otherwise, if it’s fine and you have no other worries about the changes, I can open a PR for it. Thanks
Steps to Reproduce
- Make an
ObjectId
type with bson’s definition and mongoose’s definition - Define a Schema using the
ObjectId
above as its_id
type - Query that Model and
lean
the result. - Attempt to use the
.equals
method with a string or another variable.
import { ObjectId } from "bson";
import { Schema } from "mongoose";
type Book = {
_id: ObjectId & Schema.Types.ObjectId;
title: string;
}
const BookSchema = new Schema<Book>({ title: { type: String } });
const BookModel = model("book", BookSchema);
async function run() {
const { _id } = await BookModel.create({
title: "Book Title 👍"
});
const res = await BookModel.findOne({}).lean();
res?._id.equals(_id) // TS Error
}
run();
Expected Behavior
Not to have the Typescript error.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
I thought about it, but
& { _id: T['_id'] }
is actually important because that makes_id
required in the output. Some devs may make_id
optional in their document definition, although we don’t recommend that.@vkarpov15
After I reposted the script, I tried the
Types.ObjectId
definition and it was exactly what I was trying to get from extending withbson
’s definitions. I don’t know how I forgot to comment on it, so I’m sorry for that.With that said, I’ll appreciate if you still consider improving the visibility of the lean type when the variable is hovered over. It’s not as important, but personally it’s something that I feel will be massively appreciated.
Thanks again for your work on the package.