Skip to content

Commit

Permalink
Refine query selector to highlight unexpected query keys
Browse files Browse the repository at this point in the history
Previously, there was a catch-all case in RootQuerySelector which would accept any key with type "any". This still allows nice IDE typehints, but can lead to bugs where the incorrect key is used for a filter with no warning that the key is clearly wrong.

With this PR, only keys that contain a "." are unsafely typed as any, which should reduce the blast radius of the hacky types dramatically.
  • Loading branch information
alex-statsig authored Jul 26, 2024
1 parent f921c9a commit 330dd36
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ declare module 'mongoose' {
/** @see https://www.mongodb.com/docs/manual/reference/operator/query/comment/#op._S_comment */
$comment?: string;
// we could not find a proper TypeScript generic to support nested queries e.g. 'user.friends.name'
// this will mark all unrecognized properties as any (including nested queries)
[key: string]: any;
// this will mark all unrecognized properties as any (including nested queries) only if
// they include a "." (to avoid generically allowing any unexpected keys)
[nestedSelector: `${string}.${string}`]: any;
};

interface QueryTimestampsConfig {
Expand Down

0 comments on commit 330dd36

Please sign in to comment.