Bill
Bill

Reputation: 5150

How to declare the correct Express Err Type

Im defining the err as any because I dont know how to define it correctly. I expected to find an express.Error type but there wasn't one.

How should i set the type for err?

// Catch Syntax Error in JSON
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
  if (err.status === 400 && err instanceof SyntaxError && 'body' in err) {
    res.status(200).send({ message: 'JSON Syntax Error' });
  } else {
    next();
  }
});

Upvotes: 0

Views: 84

Answers (1)

Colliot
Colliot

Reputation: 1551

The err is regarded as any according to the official type definition here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/6b3f9450aa2ced2bae7851acebc5ed9d7e6200c2/types/express-serve-static-core/index.d.ts#L45

So I think it's ok to use any here.

Upvotes: 2

Related Questions