Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter query Condition type might as well be any #14397

Closed
2 tasks done
sderrow opened this issue Mar 1, 2024 · 3 comments · Fixed by #14436
Closed
2 tasks done

Filter query Condition type might as well be any #14397

sderrow opened this issue Mar 1, 2024 · 3 comments · Fixed by #14436
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@sderrow
Copy link
Contributor

sderrow commented Mar 1, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.2.0

Node.js version

18.18.0

MongoDB server version

7.0

Typescript version (if applicable)

4.5.2

Description

The following appears in query.d.ts:

export type ApplyBasicQueryCasting<T> = T | T[] | (T extends (infer U)[] ? U : any) | any;
type Condition<T> = ApplyBasicQueryCasting<T> | QuerySelector<ApplyBasicQueryCasting<T>>;

type _FilterQuery<T> = {
  [P in keyof T]?: Condition<T[P]>;
} & RootQuerySelector<T>;

The | any union in ApplyBasicQueryCasting<T>, means that ApplyBasicQueryCasting is any, which carries through to Condition<T> being any as well.

I think that defeats the purpose of the Condition type in the first place, since now there's no support for prompting the user with valid Mongo operators like $regex and $gte or displaying type errors if you try to pass an invalid value into such an operator (e.g., { $regex: 402 } or { $exists: "hello" }).

Steps to Reproduce

type Test = {
  name: string;
  age: number;
};

// No Typescript error below even though `$regex` cannot be passed a number
// Additionally, there's no auto-complete help for the values of the query object
export const query: FilterQuery<Test> = { name: { $regex: 10 } };

Expected Behavior

Additional Typescript support when it comes to writing filter queries. I understand there's a need for any, but is it possible to do it without sacrificing the operator help?

@FaizBShah
Copy link
Contributor

@sderrow I think I do have a solution on fixing this, but is there any condition where the any type will be necessary? As far as I know about the filters, I cannot personally remember about any condition which might require the use of any type. But maybe I might be wrong, so I'll like to know.

@FaizBShah
Copy link
Contributor

I did find a few cases where any will be required. In this case, its required to do ObjectId casting:

Test.find({ parent: '0'.repeat(24) });
Test.find({ parent: { $in: ['0'.repeat(24)] } });

@FaizBShah
Copy link
Contributor

@sderrow I created a PR to fix this. Do comment any thoughts you have about it? - #14398

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
4 participants