Skip to content

types: correct handling of _id in ProjectionType #15432

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

Merged
merged 1 commit into from
May 25, 2025
Merged

Conversation

vkarpov15
Copy link
Collaborator

Summary

Quick fix for #15418: looks like we need to explicitly Omit<T, '_id'> to avoid TypeScript getting tripped up by _id in InclusionProjection and ExclusionProjection.

Examples

@vkarpov15 vkarpov15 added this to the 8.15.1 milestone May 23, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses issue #15418 by correcting the handling of _id in ProjectionType and ensuring that both inclusion and exclusion scenarios for _id are properly tested.

  • Adds tests to verify explicit inclusion/exclusion of _id in projections.
  • Ensures that common projection usage with _id behaves as expected in TypeScript.

@vkarpov15
Copy link
Collaborator Author

This will fix #15431

@DavideViolante
Copy link
Contributor

Just a question about the tests, what's the difference between line 164, 167 and the newest 190-191? Seems like all of them check with _id: 0 and another prop: 1. Why line 164 or 167 passed the test before this PR?

@hasezoey hasezoey linked an issue May 25, 2025 that may be closed by this pull request
2 tasks
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Just a question about the tests, what's the difference between line 164, 167 and the newest 190-191? Seems like all of them check with _id: 0 and another prop: 1. Why line 164 or 167 passed the test before this PR?

At least line 191 is new as there previously was no _id: 1 test.

After some slight looking around and guess-work, it might be related to Model.find making use of ProjectionType<TRawDocType> where in the tests TRawDocType (interface ITest) does not include a _id property, but the example interface IUser in #15431 does. So my best guess is that in the tests it worked due to the & AnyObject clause in ProjectionType.

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label May 25, 2025
@vkarpov15
Copy link
Collaborator Author

My best guess would be because existing tests have multiple fields with : 1, whereas the new test only has 1 field with : 1. TypeScript is full of surprising quirks and behaviors.

@vkarpov15 vkarpov15 merged commit f0736a9 into master May 25, 2025
6 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15431 branch May 25, 2025 18:15
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

Successfully merging this pull request may close these issues.

Excluding _id and including another prop in projection returns TS error
3 participants