Skip to content

Conversation

vkarpov15
Copy link
Collaborator

Fix #12064

Summary

Renamed FilterQuery -> QueryFilter to be more idiomatic, and also added WithLevel1NestedPaths to get us 1-level deep nested paths for better autocomplete. I needed to make some fixes to WithLevel1NestedPaths to better handle document arrays and other edge cases.

Screenshot from 2025-08-18 13-09-41 Screenshot from 2025-08-18 13-10-00

Also removed RootFilterQuery.

Examples

@vkarpov15 vkarpov15 added this to the 9.0 milestone Aug 18, 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 implements a breaking change by renaming FilterQuery to QueryFilter to follow more idiomatic TypeScript naming conventions, and adds 1-level deep nested path support via WithLevel1NestedPaths for improved autocomplete functionality. The changes also remove the deprecated RootFilterQuery type.

  • Renamed FilterQuery type to QueryFilter throughout the codebase
  • Enhanced WithLevel1NestedPaths type utility to better handle document arrays and edge cases
  • Updated migration documentation to reflect the breaking changes

Reviewed Changes

Copilot reviewed 4 out of 9 changed files in this pull request and generated 2 comments.

File Description
test/types/sanitizeFilter.test.ts Updates import and type expectation to use QueryFilter instead of FilterQuery
test/types/queries.test.ts Comprehensive update of all FilterQuery references to QueryFilter, adds nested path testing, and includes new test case for gh12064
test/types/models.test.ts Updates WithLevel1NestedPaths type tests with improved type assertions and optional property handling
docs/migrating_to_9.md Updates migration documentation to reflect the FilterQuery to QueryFilter rename and related breaking changes

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

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.

Good change, but something with generics in this PR causes infinite type loops for mongoose.Schema<any>:

src/typegoose.ts:103:46 - error TS2589: Type instantiation is excessively deep and possibly infinite.

103   const compiledModel: mongoose.Model<any> = modelFn(name, buildSchema(cl, mergedOptions));
                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/typegoose.ts:192:11 - error TS2589: Type instantiation is excessively deep and possibly infinite.

192     sch = _buildSchema(parentClass, sch!, mergedOptions, false, undefined, extraOptions);
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/typegoose.ts:465:9 - error TS2589: Type instantiation is excessively deep and possibly infinite.

465   const sch: mongoose.Schema<any> = buildSchema(cl, mergedOptions);
            ~~~

src/typegoose.ts:493:9 - error TS2589: Type instantiation is excessively deep and possibly infinite.

493   const sch: mongoose.Schema<any> = {} as mongoose.Schema<DocumentType<InstanceType<U>>>;

Note that the error for line 493 is simply a repro try:

type AnyParamConstructorT<T> = new (...args: any) => T;
type DocumentTypeT<T> = mongoose.Document<unknown, {}, T> & mongoose.Default__v</* mongoose.Require_id<T> */ T>;

function test<U extends AnyParamConstructorT<U>>(cl: U) {
  const sch: mongoose.Schema<any> = {} as mongoose.Schema<DocumentTypeT<InstanceType<U>>>;
  console.log(sch);
}

(the error does not happen if mongoose.Default__v or mongoose.Require_id is not in the chain, if either is present, the error occurs)

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

@hasezoey can you please try again? With my fix in 9f40ebf, the following compiles

type AnyParamConstructorT<T> = new (...args: any) => T;
type DocumentTypeT<T> = mongoose.Document<unknown, {}, T> & mongoose.Default__v</* mongoose.Require_id<T> */ T>;

function test<U extends AnyParamConstructorT<U>>(cl: U) {
  const sch: mongoose.Schema<any> = {} as mongoose.Schema<DocumentTypeT<InstanceType<U>>>;
  console.log(sch);
}

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.

Works without any extra changes or errors with 9f40ebf in typegoose.

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

Successfully merging this pull request may close these issues.

3 participants