Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
How to fix extended property on express request

I'm building a middleware in javascript where I can get the information of the user by using the famous "req" of the route handler. When i do a console.log(currentUser), I get the expected value but the currentUser is giving a typescript error warning: Property 'currentUser' does not exist on type 'Request<{}, any, any, ParsedQs, Record<string, any>>'.ts(2339)

Below is my piece of code:

middleware

const currentUser = async (req, res, next) => {
  if (!req.cookies?.token) return next();

  try {
    const payload = jwt.verify(req.cookies.token, process.env.JWT_SECRET);

    req.currentUser = payload;
  } catch {}

  next();
};

exports.currentUser = currentUser;

currentUser

const { currentUser } = require("../middleware");

router.get("/users/currentuser", currentUser, (req, res) => {
  const { currentUser } = req;
  console.log(currentUser);
});

exports.currentUser = router;

and this is the result when I do the console.log:

{ id: '625c2298ec690d8db4c6b37d', email: 'test@mail.com', role: 'user', name: 'Martin', iat: 1650218344, exp: 1650304744 }

Can some one help me out fixing this in javascript because i'm not familiar with typescript.

Thank you in advanced!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The Request type of Router contains some fixed properties.

If you want to add your own custom properties to it, either use

req.currentUser = payload;

OR

Create a type for own use like below, and use it for req

export type CustomRequest = ExpressRequest & {
  currentUser?: string | JwtPayload;
};

Hope this helps

about 4 years ago · Juan Pablo Isaza Denunciar

0

for this one you can use Type.

type NewRequest = Request & {
  currentUser: string | JwtPayload,
}

async (req:NewRequest, res:Response, next:NextFunction)

I Hope, this will help you. Thanks)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda