overnight
overnight copied to clipboard
How to get type safe on req.params
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"]
}
although I never realized it, I usually validate the params, which in your case would be:
const { idWrong } = req.params
if (!reallyWrong) {
// handler error
}