Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

379
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

Bit of a dirty solution:

childRouter.get('/', (req, res) => {
  const { username } = req.params as any;
  …
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda