属性'locals'在'Response'类型上不存在

问题描述 投票:-1回答:1

我想使用res.locals在中间件之间传递一些数据,但打字稿说我不能。 :(

我正在使用Restify。我需要插件吗?如果不是这样,将赞赏在中间件之间传递数据的替代解决方案。

node.js typescript restify
1个回答
0
投票

⚠️如果使用打字稿,则会出现错误,因为locals不属于Interface res的一部分。

因此,如果要添加自己的属性(例如,在res中,则可以这样:res['locals'] = { name: 'John doe' }

📤更新:添加属性以重新验证res

要为您的restify locals声明Response属性,可以使用下面的代码:

declare module 'restify' {
  interface Response{
    locals: any
  }
}

[声明了自己的属性后,在Restify响应中为示例locals,因此,现在您可以在Restify locals中使用response

我希望它能为您提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.