How to add Typescript definitions to Express req & res

后端 未结 4 1900
梦毁少年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:17

    The best way to do this is like so.

    // create some shared types in your project

    import { Request, Response, NextFunction } from 'express';
    export type MiddlewareFn = (req: Request, res: Response, next: NextFunction) => void;
    

    // then use the above types:

    import {MiddlewareFn} from './my-types.d.ts'
    
    router.get('/foo', function (req, res, next) {
    
       // ....
    });
    

提交回复
热议问题