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?
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
Bit of a dirty solution:
childRouter.get('/', (req, res) => {
const { username } = req.params as any;
…
});