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

381
Vistas
Using Express Mergeparams with TypeScript

I'm using Typescript for my small project and I'm encountering a problem. I'm nesting my router but Typescript doesn't seem to recognize the parent's parameter.

On the "child" file I have

const childRouter = express.Router({ mergeParams: true });

childRouter.get('/', (req, res) => {
    const userName = req.params.username; 
    // This causes the error, Property 'username' does not exist on type '{}'
});

and then on the "parent" file the code is

import childRouter from './child';

const parentRouter = express.Router();

parentRouter.use('/:username', childRouter);

I have no idea how to fix this, it seems like typescript doesn't recognize that I'm using the parent's parameter. Any idea how to fix this?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Edit: I misread the post the first time.

The request.params should be of following type if you installed the types properly for Express.

interface ParamsDictionary {
    [key: string]: string;
}

If you need to force type, you can also do following:

const username = req.params.username as string
over 4 years ago · Santiago Trujillo Denunciar

0

Bit of a dirty solution:

childRouter.get('/', (req, res) => {
  const { username } = req.params as any;
  …
});
over 4 years ago · Santiago Trujillo 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