1

Im using node.js + express + typescript, and I want to get GET params. I have this so far

app.get('/top_games', (req, res) => {
    const page = req.route.page;
    const offset = req.route.offset;
    const game_data = DAO_GAME.GetTopGuilds(100, 0);
    res.send({
        total : game_data[0],
        data : game_data[1]
    });
});

but the eslint complains for

    const page = req.route.page;
    const offset = req.route.offset;

saying

Unsafe assignment of an any value.eslint@typescript-eslint/no-unsafe-assignment
Unsafe member access .page on an any value.eslint@typescript-eslint/no-unsafe-member-access

How can I fix this?

3
  • What does req.params give you? app.get('/user/:id', function (req, res) { res.send('user ' + req.params.id) }) Commented Aug 10, 2020 at 2:39
  • req.params gets me {}, the data is in req.query Commented Aug 10, 2020 at 2:47
  • expressjs.com/en/api.html#req.query Commented Aug 10, 2020 at 2:48

2 Answers 2

1

Fixed it, I had to do

const page = parseInt(req.query.page as string, 10);
const page_size = parseInt(req.query.page_size as string, 10);
Sign up to request clarification or add additional context in comments.

Comments

1

As of @types/[email protected], the Request type uses generics. We can use this to type the parsed query parameters ourselves:

import { Request } from 'express';

type QueryParams = {
    page: string;
    page_size: string;
};

app.get('/top_games', (req: Request<{}, {}, {}, QueryParams>, res) => {
    const page = parseInt(req.route.page, 10);
    const offset = parseInt(req.route.offset, 10);

    // ...
});

1 Comment

Thank you for answering this, I've just spent the last few days trying to figure this out and this is exactly what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.