How to add Typescript definitions to Express req & res

后端 未结 4 1891
梦毁少年i
梦毁少年i 2021-02-07 04:57

I have a set of controller functions for my REST API and I\'m getting lots of the following

error TS7006: Parameter \'req\' implicitly has an \'any\' type.
         


        
4条回答
  •  不知归路
    2021-02-07 05:22

    You can use ES6 style named imports to import only the interfaces you need, rather than import * as express from 'express' which would include express itself.

    First, make sure you have installed the type definitions for express (npm install -D @types/express).

    Example:

    // middleware/authCheck.ts
    import { Request, Response, NextFunction } from 'express';
    
    export const authCheckMiddleware = (req: Request, res: Response, next: NextFunction) => {
      ...
    };
    
    // server.ts
    import { authCheckMiddleware } from './middleware/authCheck';
    app.use('/api', authCheckMiddleware);
    

    Currently using TypeScript 2.3.4 and @types/express 4.0.36.

提交回复
热议问题