Make typescript fail when providing unknown query operator#16084
Open
Make typescript fail when providing unknown query operator#16084
Conversation
2 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens Mongoose’s TypeScript query filter typings so that unknown field-level query operators (for example $wrong) fail at compile time, aligning TypeScript behavior more closely with Mongoose’s runtime casting behavior.
Changes:
- Replace usage of the MongoDB Node driver’s permissive
Conditiontype with a stricter condition type that removes the driver’s string index signature fromFilterOperators. - Add a TypeScript type test asserting unknown query operators are rejected.
- Update Number schema query casting to throw a consistent “Can’t use $op with Number.” error and add a runtime test for it.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| types/query.d.ts | Introduces StrictCondition (via index-signature removal) and applies it to QueryFilter to reject unknown operators in TS. |
| test/types/queries.test.ts | Adds a type-level regression test ensuring { $wrong: ... } fails to compile for filters. |
| lib/schema/number.js | Updates Number’s castForQuery() to throw a consistent unsupported-operator error message. |
| test/types.number.test.js | Adds a runtime regression test asserting unsupported operator usage throws with the expected message. |
Collaborator
AbdelrahmanHafez
left a comment
There was a problem hiding this comment.
One suggestion, otherwise LGTM
Co-authored-by: Hafez <a.hafez852@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Right now providing an unknown query operator like
$wrongpasses in TypeScript, but throws a runtime error. 937eeb1 fixes a related issue where the error message is inconsistent for numbers vs other schematypes.269afd9 updates it so Mongoose doesn't use MongoDB Node driver's
Conditiontype, which intentionally allows string signature. Instead, we strip out the string signature so only known query operators are allowed.Examples