Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Express Error Function

What are the types of the four parameters for the error handling function when coding in Typescript?

app.use((err: ??, req: ??, res: ??, next: ??) => { });

I am using VS Code and no hints are offered. I get red wiggly lines under all four parameters.

The error says "Parameter implicitly has 'any' type". Actually I am confused with this message. If it treats it as an any type, then isn't that a valid choice?

like image 442
Old Geezer Avatar asked May 07 '18 16:05

Old Geezer


People also ask

How do you handle errors in Express?

The simplest way of handling errors in Express applications is by putting the error handling logic in the individual route handler functions. We can either check for specific error conditions or use a try-catch block for intercepting the error condition before invoking the logic for handling the error.

How do I catch error in middleware Express?

For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them. For example: app. get('/', function (req, res, next) { fs.

How do I use Express Async error?

To handle an error in an asynchronous function, you need to catch the error first. You can do this with try/catch . Next, you pass the error into an Express error handler with the next argument. If you did not write a custom error handler yet, Express will handle the error for you with its default error handler.

What is next error in Express?

If you pass anything to the next() function (except the string 'route' ), Express regards the current request as being an error and will skip any remaining non-error handling routing and middleware functions. If the callback in a sequence provides no data, only errors, you can simplify this code as follows: app.


1 Answers

The function itself has the following signature (taken from DefinitelyTyped):

export type ErrorRequestHandler = (err: any, req: Request, res: Response, next: NextFunction) => any;

So you can either declare the function as a variable of type ErrorRequestHandler or type the parameters according to that definition.

Note: the typings for "express-serve-static-core" are imported and re-exported by the typings for "express", which was where I looked for the above definition.

import type { ErrorRequestHandler } from "express";
const errorHandler: ErrorRequestHandler = (err, req, res, next) => {};

app.use(errorHandler);

Regarding your second question related to implicit any, it is the "implicit" part that is causing the problem, If you explicitly type as any then there won't be any error (but there won't be any typings either; consider using unknown instead).

You can also disable noImplicitAny in your compiler config but I wouldn't recommend it personally, as it protects you from several classes of bugs.

like image 149
Tom Fenech Avatar answered Oct 13 '22 18:10

Tom Fenech