To fix the lint errors in your code, you need to address the TypeScript type issues that arise from dynamically building Convex queries. The main problem is that after applying a search index, the type of the query changes, and you can't use a single variable for all stages of building the query without explicit typing. This is a common issue in Convex, and the recommended solution is to use separate variables for each stage of the query, as described in the Convex dynamic query builder pattern Convex Cookbook: Dynamic Query Builders.
Here's how you can refactor your code to avoid lint/type errors:
import { OrderedQuery } from "convex/server";
let query;
if (args.searchQuery) {
query = ctx.db
.query("tokens")
.withSearchIndex("search_ticker", (q) =>
q.search("ticker", args.searchQuery!)
);
} else {
query = ctx.db.query("tokens");
}
let orderedQuery: OrderedQuery<any>;
orderedQuery = query.order("desc");
const results = await orderedQuery.paginate(args.paginationOpts);
const page = await Promise.all(
results.page.map(async (token) => {
let imageUrl: string | null = null;
if (token.imageId) {
imageUrl = await ctx.storage.getUrl(token.imageId);
}
return {
...token,
imageUrl,
};
})
);
Key points:
- Use separate variables for each stage of the query (
query, then orderedQuery).
- Avoid reassigning a variable to a different query type.
- This pattern is recommended by Convex to avoid TypeScript errors when building queries dynamically Convex Cookbook: Dynamic Query Builders.