title: How can I handle type with middleware of express?
body: <p>I am using Typescript in Node.js. When you use Express middleware, you often transform the Request object. With Typescript, however, we could not track how the Request object was transformed. If you know the middleware that passed before, is there a way to find out the type of the request from it? If not possible in express, I would like to find another framework where it is possible. Is it possible in Nest (<a href="https://github.com/kamilmysliwiec/nest" rel="noreferrer">https://github.com/kamilmysliwiec/nest</a>)?</p>
  
  <p>Example Code</p>
  
  <pre><code>import { Request, Response, NextFunction } from 'express';
  
  function userMiddleware(req: Request &amp; User, res: Response, next: NextFunction) {
      req.user = {
          id: 'user_id',
      };
      next();
  }
  
  interface User {
      user: {
          id: string;
      }
  }
  
  interface Middleware {
      &lt;T&gt;(req: Request &amp; T, res: Response, next: NextFunction): void;
  }
  
  class Controller {
      middleware = [userMiddleware];
  
      get = new GetMethod(this.middleware);
  
      post = (req: Request /* I don't know exact req type */, res: Response, next: NextFunction) =&gt; {
          console.log(req.user) // Error!
      }
  }
  
  class GetMethod {
      constructor(middleware: Middleware[]) {
          // How to deduce type of req from Middleware array?
      }
  }
  
  const controller = new Controller();
  
  express.use('/', controller.middleware, controller.post);
  </code></pre>
  
  <p>I want to extract type information from Middleware list in Controller class.</p>