What is the best way to define my own custom types definations?

I am trying to build some application with typescript. I was facing issues with some types like for example setting a status code for internal server error when I catch some error in the try block and setting

./src/controllers/admin.ts
const handleServerError = (error: unknown | any, next: NextFunction) => {
  let errorMessage: string;

  if (error instanceof Error) {
    errorMessage = error.message;
  } else {
    errorMessage = String(error);
  }

  const err = new Error(errorMessage);
  console.error(err);
  err.httpStatusCode = 500;
  return next(err);
};

and using it like a call in the catch block:
try {
} catch (error) {
  handleServerError(error, next);
}

Since, httpStatusCode does not exists on the type Error. I solved this issue by creating ./src/types/error.d.ts
declare global {
  interface Error {
    httpStatusCode?: number;
  }
}

export {};

However I have to add this defination file in my tsconfig.json
{
"compilerOptions": {
},
"files": [
"src/types/error.d.ts",
...
]
}

Is there any better ergonomic way to deal with this situation?
Was this page helpful?