Skip to content
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: Wrong return type when using lean in different way #13705

Closed
2 tasks done
GuiHash opened this issue Aug 8, 2023 · 2 comments · Fixed by #13721
Closed
2 tasks done

Typescript: Wrong return type when using lean in different way #13705

GuiHash opened this issue Aug 8, 2023 · 2 comments · Fixed by #13721
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@GuiHash
Copy link

GuiHash commented Aug 8, 2023

Prerequisites

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

Mongoose version

6.X.X - 7.X.X

Node.js version

at least 16

MongoDB server version

at least 4

Typescript version (if applicable)

4.7 - latest

Description

The return type of

const a = await Model.findById('my-id', undefined, { lean: true })

is the same as

const a = await Model.findById('my-id')

it's:

// ^? const a: (mongoose.Document<unknown, {}, { name: string; }> & { name: string; } & { _id: mongoose.Types.ObjectId; }) | null

Steps to Reproduce

See ts playground

Expected Behavior

The return type should be the same as

const a = await Model.findById('a').lean()

it's:

// ^? const a: (mongoose.FlattenMaps<{ name: string; }> & { _id: mongoose.Types.ObjectId; }) | null
@vkarpov15 vkarpov15 added this to the 7.4.4 milestone Aug 8, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 8, 2023
@vkarpov15
Copy link
Collaborator

We'll investigate and see if it is reasonably possible to fix. As a workaround, use Model.findById('a').lean().

vkarpov15 added a commit that referenced this issue Aug 11, 2023
…h `lean: true` to relevant model functions like `find()` and `findOne()`

Fix #13705
@vkarpov15 vkarpov15 modified the milestones: 7.4.4, 7.4.3 Aug 11, 2023
vkarpov15 added a commit that referenced this issue Aug 11, 2023
types(models+query): return `lean` type when passing QueryOptions with `lean: true` to relevant model functions like `find()` and `findOne()`
@sspenst
Copy link

sspenst commented Oct 17, 2023

For reference, I just updated from mongoose 7.4.2 to 7.6.2 and had to change every occurrence of:

TModel.findOne<T>({}, {}, { lean: true })
TModel.find<T>({}, {}, { lean: true })

to:

TModel.findOne().lean<T>()
TModel.find().lean<T[]>()

sspenst/thinky.gg@8c3c744

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
3 participants