Skip to content

types: handle nested projections in typescript - #16678

Merged
vkarpov15 merged 4 commits into
9.13from
vkarpov15/gh-16576
Nov 9, 2026
Merged

vkarpov15 merged 4 commits into
9.13from
vkarpov15/gh-16576

Conversation

@vkarpov15

Copy link
Copy Markdown
Collaborator

Fix #16575
Re: #16576

Summary

Currently Model.find({}, { 'nested.subpath': 1 }) excludes nested from 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': 1 now excludes num if num is a number path or any other scalar.

Attached is claude-generated blog post summarizing the changes:

apply-projection-post.md

Examples

@vkarpov15 vkarpov15 added this to the 9.13 milestone Nov 4, 2026
@vkarpov15
vkarpov15 requested review from AbdelrahmanHafez and hasezoey and a lite review from Copilot November 4, 2026 18:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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

@hasezoey hasezoey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Nov 5, 2026

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Other than Copilot's comment, LGTM. I confirmed that the $meta issue is a regression.

@vkarpov15
vkarpov15 changed the base branch from master to 9.13 November 9, 2026 20:35
@vkarpov15
vkarpov15 merged commit 83dfcf4 into 9.13 Nov 9, 2026
43 checks passed
@hasezoey
hasezoey deleted the vkarpov15/gh-16576 branch November 10, 2026 11:16
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.

types: projection narrowing in 9.11 drops fields selected via dotted inclusion paths ("a.b": 1)

4 participants