title: I face casting error while trying filter with Mongoose. Please! check
body: <p>it's my first question here so, I hope I hit the context and describe this as well as I could.</p>
  <p>I have that function that provides my API handler(controller/route handler) with all the fancy stuff of (filtering/sorting/pagination) but, it works with 2 endpoints perfectly and doesn't work with that particular endpoint.</p>
  <p>The code of that service function:</p>
  <pre><code>export async function paginate&lt;ModelInterface&gt;(
    Model: mongoose.Model&lt;ModelInterface&gt;,
    {
      filters = {},
      limit = 10,
      page = 1,
      sort = {},
    }: PaginateOptions&lt;ModelInterface&gt; = {}
  ) {
    try {
      const skip = (page - 1) * limit;
      const list = await Model.find(filters)
        .sort(sort)
        .skip(skip || 0)
        .limit(limit || 0);
      const total = await Model.count(filters);
      const count = list.length;
      const hasMore = count + skip &lt; total;
      const thisPage = page;
      const nextPage = hasMore ? page + 1 : null;
      const lastPage = Math.ceil(total / limit);
      return { list, total, count, hasMore, thisPage, nextPage, lastPage };
    } catch (error) {
      throw error;
    }
  }
  </code></pre>
  <p>I started working on the project and it was working with this Endpoint (users).</p>
  <p>Here's the user.service:</p>
  <pre><code>export async function findUsers(options: PaginateOptions&lt;UserDocument&gt; = {}) {
    const allowPartialMatch = [&quot;company&quot;, &quot;email&quot;, &quot;website&quot;, &quot;name&quot;];
    console.log(options);
    try {
      const filters =
        options.filters &amp;&amp;
        Object.entries(options.filters).reduce(
          (ret, [key, value]) =&gt; ({
            ...ret,
            [key]: allowPartialMatch.includes(key)
              ? { $regex: value, $options: &quot;i&quot; }
              : value,
          }),
          {}
        );
      const query = { ...options, filters };
      return await paginate&lt;UserDocument&gt;(UserModel, query);
    } catch (error) {
      throw error;
    }
  }
  </code></pre>
  <p>and user.controller which will be applied to the route handler:</p>
  <pre><code>export async function getAllUsersHandler(
    req: Request&lt;{}, {}, {}, { filters: string; page: string; sort: string }&gt;,
    res: Response,
    next: NextFunction
  ) {
    try {
      const { query } = req;
  
      const filters = JSON.parse(
        query.filters || &quot;{}&quot;
      ) as FilterQuery&lt;UserDocument&gt;;
      const sort = JSON.parse(query.sort || &quot;{}&quot;) as {
        [key: string]: SortValues;
      };
      const data = await findUsers({
        filters,
        page: parseInt(query.page) || 1,
        sort,
      });
      return res.json(data);
    } catch (error: any) {
      next(error);
    }
  }
  </code></pre>
  <p>Once I started working on the project my task was working on another endpoint with the same structure and I applied the same code for another endpoint with the modifications and It worked perfectly.
  but for this endpoint, it Gave me this Error which I can't solve. Please! help.</p>
  <p>redeem.service:</p>
  <pre><code>export async function listRedeems(
    options: PaginateOptions&lt;RedeemDocument&gt; = {}
  ) {
    const allowPartialMatch = [&quot;amount&quot;, &quot;user&quot;, &quot;transaction_id&quot;, &quot;approved&quot;];
    console.log(options);
    try {
      // FIXME: it Produce a &quot;Cast Error&quot; complaining About &quot;i&quot; property Type.
      // ERR-TRACE: https://noncebloxworkspace.slack.com/files/U02QT3XJBK5/F02S9JJAAP4/error_log.png
      const filters =
        options.filters &amp;&amp;
        Object.entries(options.filters).reduce(
          (ret, [key, value]) =&gt; ({
            ...ret,
            [key]: allowPartialMatch.includes(key)
              ? { $regex: value, $options: &quot;i&quot; }
              : value,
          }),
          {}
        );
      const query = { ...options, filters };
      return await paginate&lt;RedeemDocument&gt;(RedeemModel, query);
    } catch (error) {
      console.log(error);
      throw error;
    }
  }
  </code></pre>
  <p>redeem.controller:</p>
  <pre><code>export async function listRedeemsHandler(
    req: Request&lt;{}, {}, {}, { filters: string; page: string; sort: string }&gt;,
    res: Response,
    next: NextFunction
  ) {
    try {
      const { query } = req;
  
      const filters = JSON.parse(
        query.filters || &quot;{}&quot;
      ) as FilterQuery&lt;RedeemDocument&gt;;
      const sort = JSON.parse(query.sort || &quot;{}&quot;) as {
        [key: string]: SortValues;
      };
  
      const data = await listRedeems({
        filters,
        page: parseInt(query.page) || 1,
        sort,
      });
      return res.json(data);
    } catch (error) {
      next(error);
    }
  }
  </code></pre>
  <p>Here You Are the HTTP Request sent (Only filters Crashes):</p>
  <blockquote>
  <p>{{base_url}}/redeem?page=1&amp;sort={&quot;updatedAt&quot;: &quot;-1&quot;}&amp;filters={&quot;amount&quot;:
  50}</p>
  </blockquote>
  <p>and Here You Are the Error Log:</p>
  <p><a href="https://i.stack.imgur.com/qh8tV.png" rel="nofollow noreferrer">Here You Are the Log's Error</a></p>