Repository navigation
types: handle nested projections in typescript - #16678
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
An active moderate type-level bug causes projections combining _id with $meta to incorrectly drop fields such as score, so the inclusion-key logic needs correction and coverage for this combination.
No open issues 🎉
What changed in this PR
This pull request improves TypeScript projection inference for nested paths, including subdocuments, document arrays, maps, arrays, scalar fields, and projection operators.
Changes:
- Adds recursive helpers for nested inclusion and exclusion projections.
- Preserves compatible container and hydrated document types while narrowing projected fields.
- Expands compile-time coverage for nested paths, arrays, maps, scalar edge cases,
$slice, and related operators.
A moderate issue remains: projections combining _id with $meta fields can incorrectly omit the $meta field from the inferred result type. This pull request requires changes.
| File | Summary |
|---|---|
types/utility.d.ts |
Implements recursive nested projection narrowing, scalar and container handling, and updated projection-mode detection. |
test/types/queries.test.ts |
Adds compile-time coverage for nested projections, arrays, subdocuments, document arrays, maps, scalar paths, and projection operators. |
Review effort: Lite
AbdelrahmanHafez
left a comment
There was a problem hiding this comment.
Other than Copilot's comment, LGTM. I confirmed that the $meta issue is a regression.
Fix #16575
Re: #16576
Summary
Currently
Model.find({}, { 'nested.subpath': 1 })excludesnestedfrom the resulting projection. This PR handles that case for all container paths: subdocuments, document arrays, maps, nested paths. Also improves some edge case handling, e.g. projecting'num.toString': 1now excludesnumifnumis a number path or any other scalar.Attached is claude-generated blog post summarizing the changes:
apply-projection-post.md
Examples