Don't use non-explicit TypeScript Types
TypeScript allows you to define types in different ways and through a selection of primitives such as ‘string’, ‘number’, ‘object’ and others but it also allows you to opt-out and use non-explicit types such as any
, unknown
, and never
. When you use these types you are essentially telling TypeScript to ignore any type checking and avoid forcing type safety.
Avoid using any
The following shows an example declaration of the variable filterQuery
with the type any
. This is a bad practice as it allows the variable to be assigned any type of value:
import type { Request, RequestHandler, Response } from "express";
import { userService } from "@/api/user/userService";import { handleServiceResponse } from "@/common/utils/httpHandlers";
class UserController { public getUsers: RequestHandler = async (_req: Request, res: Response) => { const filterQuery: any = _req.query.filter || ''; const serviceResponse = await userService.findAll({ filter: filterQuery }); return handleServiceResponse(serviceResponse, res); };}
Similarly, you should also avoid using unknown
and never
types as they are not explicit and can lead to development-time and to runtime errors.