How to add Typescript definitions to Express req & res

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

    Rather than installing types(@types/express) you should also define request parameters. Since every parameter is string, interface should base on dictionary.

    Here is an inline route handler:

    interface GetParams {
      [key: string]: string
      paramName: string
    }
    
    router.get('/:paramName', (req, res) => {
      res.send('Parameter is ' + req.params.paramName)
    })
    
    

提交回复
热议问题