I am currently in the process of transitioning my express server to Typescript. Below is a simple example showcasing this migration.
One challenge I encountered was determining the appropriate type for the argument of the error handler handleServerErrors
.
Initially, I assigned it as Error
, but TypeScript flagged an error stating that error.code
is not part of Error. As a workaround, I reluctantly used any
just to proceed.
It appears that TypeScript is referencing the JavaScript definition of Error instead of the Node.js one.
I can confirm that I have indeed installed @types/node
import express = require("express");
const handleServerErrors = (error: any) => {
if(error.code === "EADDRINUSE") {
console.log("\nServer already running.\n");
}
else {
console.error(error);
throw error;
}
};
const app = express();
app
.listen(4400, () => console.log("Running")
.once("error", handleServerErrors);