ExpressJs with TypeScript - passing data between middlewares

I'm writing my first expressJs app using TypeScript. I got got static middleware method for token validation, where I need pass data to the next middleware:

    static verifyAccessToken(req: Request, resp: Response, next: NextFunction) {
        const AUTH_TOKEN = AccessTokenValidator.getTokenFromHeader(req, resp);

        jwt.verify(AUTH_TOKEN, Config.JWT_SECRET, async (error: VerifyErrors | null, payload: any) => {
            if (error) {
                resp.status(401).send({message: "Token is invalid"});
            // @ts-ignore
            req.userRole = payload.rol;

How can I pass data to next middleware correctly, without using "@ts-ignore"?


  • You can add custom express request type definition by creating a .d.ts file

    Create express.d.ts on your root project folder, and put

    declare namespace Express {
       export interface Request {
           userRole?: string // I use string for example, you can put other type