Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
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 answers
Answer question

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 Report

0

Bit of a dirty solution:

childRouter.get('/', (req, res) => {
  const { username } = req.params as any;
  …
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!