Declaring TypeScript variable as intentionally unused

#bytesize#tech
1 min read

If you enable noUnusedLocals and noUnusedParameters settings to true in your TypeScript configuration then you'll encounter a TypeScript error for any unused variables.

However, there might be instances where a variable is intentionally unused.

To declare a TypeScript variable as intentionally unused, you prefix it with an underscore.

An example would be an Express route handler where the request object is not needed:

app.get('/health', (_req: Request, res: Response) => {
  try {
       const timestamp = new Date().toISOString();
       res.json({ status: 'ok', timestamp });
  } catch (error) {
       console.error(error);
       res.status(500).json({ status: 'error', message: 'Internal server error' });
  }});