I am in the process of rewriting my JS-based express app into Typescript. While I was refactoring my router instances, I stumbled upon the following issue:
this.router.get('/tenantInfo', (req, res) => {
res.send(this.getTentantInfo(req.tenant))
})
When I want to transform this into Typescript it would look something like this:
import express, { Request, Response, Router } from 'express'
this.router.get('/tenantInfo', (req: Request, res: Response) => {
res.send(this.getTentantInfo(req.tenant))
})
which leads to an error in req.tenant
since Request
does not have this property. I searched Stackoverflow and found this answer, however, I think that this will not lead to a good application structure if I just extend the Request
interface of express with each and every additional property that I will expect in various places of my application.
What would be a nice approach to solve this in a clean and maintainable manner?