Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get type safe on req.params #81

Open
bhuynhdev opened this issue Jun 25, 2021 · 1 comment
Open

How to get type safe on req.params #81

bhuynhdev opened this issue Jun 25, 2021 · 1 comment

Comments

@bhuynhdev
Copy link

bhuynhdev commented Jun 25, 2021

The original "express" way can infer types automatically on req.params

For example:

import {Router} from "express",
const router = Router();

router.get("/:id", (req, res, next) => {
   // req is typed as Request<RouteParameters<"/:id">, any, any, QueryString.ParsedQs, Record<string, any>>
  const { idWrong } = req.params; // Intellisense catches error
  // Property 'idWrong' does not exist on type 'RouteParameters<"/:id">'
})

However, the Overnight way does not seem to offer this functionality
For example:

import { StatusCodes } from "http-status-codes";
import { Controller, Get } from "@overnightjs/core";
import { Request, Response } from "express";

const { OK } = StatusCodes;

@Controller("api/users")
export class UserController {
    @Get(":id")
    private get(req: Request, res: Response) {
        const { reallyWrong } = req.params; // Intellisense infers "reallyWrong" as string. No errors
        return res.status(OK).json({
            message: "get_called",
        });
    }
}

Is this the expected functionality of Overnight? Did I do anything wrong? And is there a way to achieve this behaviour ?

Thanks a lot.

P/s: My tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "strict": true,
    "noImplicitAny": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exlude": ["node_modules"]
}
@iago-f-s-e
Copy link

iago-f-s-e commented Aug 13, 2021

although I never realized it, I usually validate the params, which in your case would be:

const { idWrong } = req.params

if (!reallyWrong) {
// handler error
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants