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
Comments
First of all: express doesn't provide first-party typings. You are likely using Anyway, I tried to reproduce your problem using the code examples you provided and I can't. If I paste this code into an example typescript project using the following versions, it compiles just fine: "devDependencies": {
"@types/express": "^4.17.21",
"typescript": "^5.4.5"
},
"dependencies": {
"express": "^4.19.2"
} Besides that, I'm not sure what you want to do is sound in the first place. For example, you just "claim" to TypeScript that the The safe and "correct" way to do this in my opinion is to leave the |
The types are maintained by the DT folks, that said all query parameters are strings. So you need to use something to validate and derive types from them based on what you expect them to be. I do think we have interest in some things which can help here like OpenAPI schema validation and deriving types from the validated input. At some point I want to add that to the package I maintain here. That said, today we are not yet there so you will need to write code which does type assertions on your own. |
Hello, I'm running into a problem typing filter query using the express router with my request controller
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:
By getting a look to Request.ReqQuery, I need to provide a ParsedQS type, which look like this:
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
witha as GetArticleListRequestQueryParams
but it's not clean at all :/thanks for the help 🔥
The text was updated successfully, but these errors were encountered: