why request.query is not 'any' anymore? express request.query typescript error
11,708
This makes express more strict in typings. You have to add types.
const shopEventId: string = req.query.shopEventId as string
Author by
dang
Updated on June 05, 2022Comments
-
dang almost 2 years
after npm i this is the error that i get if i try to pass query params to a function that expects string: Argument of type 'string | Query | (string | Query)[]' is not assignable to parameter of type 'string'.
Type 'Query' is not assignable to type 'string'.ts(2345)import express from "express"; async function getProductsImagesByShopEvent(req: express.Request, res: express.Response, next: express.NextFunction) { try { const params = req.query; if (!params || !params.shopEventId) throw new CustomError("params are missing in /business/getProductsImagesByShopEvent", 400, "params are missing"); const shopEvent = new ShopEvent(); const events = await shopEvent.getProductsImagesByShopEvent(params.shopEventId); res.json(events); } catch (error) { next(error); } } async getProductsImagesByShopEvent(shopEventId: string) { }
the error is in params.shopEventId.. if i add: const params = (req.query as any); it works
-
JodyAndrews almost 4 yearsExpress is already providing the types. The above is just telling the compiler you think know better. The type should be checked, and definitely not be cast.
-
dang over 2 yearssomething I understood long time a go is when u commit: npm install on a repo (that has package.json - all npm packages are updated to the latest minor version