最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to add custom key to express request in typescript? - Stack Overflow

programmeradmin2浏览0评论

I tried to modify the express request inmiddleware.While I type the code there is no error and it shows signature of the request.user but while compiling it throwing error

middleware.ts

const myMiddleware = (req: Request, res: Response, next: NextFunction): void => {
  req.user = {
    userId: 1,
  }
  next()
}

src/@types/express/index.d.ts

declare namespace Express {
  export interface Request {
    user: any
  }
}

tsconfig.json

"typeRoots": ["src/@types", "node_modules/@types"],

error


error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

44           req.user = {

What is missing here or Do I need to updated the code ?

How to resolve this or any alternative solution,I do have multiple middlewares.

发布评论

评论列表(0)

  1. 暂无评论