Skip to content

Make typescript fail when providing unknown query operator#16084

Open
vkarpov15 wants to merge 4 commits intomasterfrom
vkarpov15/gh-16062
Open

Make typescript fail when providing unknown query operator#16084
vkarpov15 wants to merge 4 commits intomasterfrom
vkarpov15/gh-16062

Conversation

@vkarpov15
Copy link
Collaborator

Summary

Right now providing an unknown query operator like $wrong passes 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 Condition type, which intentionally allows string signature. Instead, we strip out the string signature so only known query operators are allowed.

Examples

Copy link
Contributor

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 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 Condition type with a stricter condition type that removes the driver’s string index signature from FilterOperators.
  • 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.

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

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

One suggestion, otherwise LGTM

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Mar 12, 2026
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

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.

4 participants