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

help typing Request query with object #5608

Open
GregLtrnr opened this issue Apr 18, 2024 · 0 comments
Open

help typing Request query with object #5608

GregLtrnr opened this issue Apr 18, 2024 · 0 comments

Comments

@GregLtrnr
Copy link

Hello, I'm running into a problem typing filter query using the express router with my request controller

//router.ts
import { listArticles } from "./controller";

const router = express.Router();

router.get("/", listArticles);
//controller.ts

export interface GetArticleListRequestQueryParams {
  page: string;
  pageSize: string;
  [key: string]: string | number | object;
}

export async function listArticles(req: Request<unknown, unknown, unknown, GetArticleListRequestQueryParams>, res: Response){
  try{
      const parameters = req.query
      const page: number = Number(parameters.page) ?? 1;
      const pageSize: number = Number(parameters.pageSize) ?? 10;
      
      if (parameters?.filter && typeof parameters.filter === "object" && "categories" in parameters.filter) {
      //...
      }
  }
}

So basically, I want to be able to get custom filters in my request, like this: GET /article?page=1&pageSize=5&filter[categories]=categId

Using this request, using a console log on req.query, i get this:

//{ page: '1', pageSize: '5', filter: { categories: 'categId' } }

This is exactly what I want, but the issue here is for typing the query param in my req argument, the param I put GetArticleListRequestQueryParams is giving me an error in the router.ts:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: Request<unknown, unknown, unknown, GetArticleListRequestQueryParams, Record<string, any>>, res: Response<any, Record<string, any>>) => Promise<...>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, unknown, unknown, ParsedQs, Record<string, any>>'.
      Type '(req: Request<unknown, unknown, unknown, GetArticleListRequestQueryParams, Record<string, any>>, res: Response<any, Record<string, any>>) => Promise<...>' is not assignable to type 'RequestHandler<ParamsDictionary, unknown, unknown, ParsedQs, Record<string, any>>'.
        Types of parameters 'req' and 'req' are incompatible.
          Type 'Request<ParamsDictionary, unknown, unknown, ParsedQs, Record<string, any>>' is not assignable to type 'Request<unknown, unknown, unknown, GetArticleListRequestQueryParams, Record<string, any>>'.
            Types of property 'query' are incompatible.
              Type 'ParsedQs' is missing the following properties from type 'GetArticleListRequestQueryParams': page, pageSizets(2769)
index.d.ts(153, 5): The last overload is declared here.

By getting a look to Request.ReqQuery, I need to provide a ParsedQS type, which look like this:

    interface ParsedQs {
        [key: string]: undefined | string | string[] | ParsedQs | ParsedQs[];
    }

So my question is, how can I give objects type to the query params for my filter attribute without making it throw an error ? (I tried putting just string, but then I get a never type on my if (parameters?.filter && typeof parameters.filter === "object" && "categories" in parameters.filter).

The only solution I found is to give the type on the const parameters with a as GetArticleListRequestQueryParams but it's not clean at all :/

thanks for the help 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants