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

v6: populate makes single document query type non-null #11041

Closed
aaronadamsCA opened this issue Dec 3, 2021 · 2 comments
Closed

v6: populate makes single document query type non-null #11041

aaronadamsCA opened this issue Dec 3, 2021 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@aaronadamsCA
Copy link

All of these should have the same type, but they do not:

import { model } from "mongoose";

const MyModel = model("MyModel");

let maybeDoc = await MyModel.findById(null).exec();
typeof maybeDoc; // (Document<any, any, unknown> & { _id: Types.ObjectId }) | null

maybeDoc = await MyModel.findById(null).populate("someField").exec();
typeof maybeDoc; // Document<any, any, unknown> & { _id: Types.ObjectId }

maybeDoc = await MyModel.findOne({}).exec();
typeof maybeDoc; // (Document<any, any, unknown> & { _id: Types.ObjectId }) | null

maybeDoc = await MyModel.findOne({}).populate("someField").exec();
typeof maybeDoc; // Document<any, any, unknown> & { _id: Types.ObjectId }

This worked as expected in Mongoose 5.13.13. During migration, ESLint flagged some "unnecessary" null checks.

Node.js 16.13.1, Mongoose 6.0.14.

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Dec 3, 2021
@IslandRhythms
Copy link
Collaborator

_id is unknown for me and there is no | null.

@aaronadamsCA
Copy link
Author

I've provided a reproduction here:

https://codesandbox.io/s/mongoose-issue-11041-q74uu?file=/index.ts

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jan 5, 2022
@vkarpov15 vkarpov15 added this to the 6.1.8 milestone Jan 5, 2022
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
Development

No branches or pull requests

3 participants