TypeScript Express Error Function

孤人 提交于 2021-02-16 16:26:13

问题


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?


回答1:


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 by the typings for "express", which was where I looked for the above definition.

Regarding your second question related to implicit any, it is the "implicit" part which 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).

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.




回答2:


Custom Error Type

Using any completely gives up the type checking advantage of the Typescript. For err parameter, it's recommended to create your own type or class like following:

// HttpException.ts in exceptions directory of your project.

export class HttpException extends Error {
  public status: number
  public message: string
  constructor(status: number, message: string) {
    super(message)
    this.status = status
    this.message = message
  }
}

And then import it for your error-handling middleware:

import { HttpException } from './exceptions/HttpException'

app.use((err: HttpException, req: Request, res: Response, next: NextFunction) => {
  err.status = 404
  err.message = 'Not Found'
  next(err)
})

We can add any properties and methods to the HttpException class as and when required, like we have added status and message.


Type Definitions

In case you haven't done already, you need to install type definitions for Node.js and Express.js for your Typescript project. This will make sure the types Request, Response and NextFunction will be recognised and auto-imported.

To install type definitions for Node.js:

npm install --save-dev @types/node

To install type definitions for Express.js:

npm install --save-dev @types/express

That's it! Hope that helps.




回答3:


I don't know why Typescript does not get that we are passing an error handler.

These are the solutions I found possible :

app.use((err, req, res, next) => {}) // KO    

const inVariable : ErrorRequestHandler = (err, req, res, next) => {};

app.use(inVariable); // ok
app.use(((err, req, res, next) => {}) as ErrorRequestHandler); // ok


来源:https://stackoverflow.com/questions/50218878/typescript-express-error-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!