Search code examples
node.jstypescriptrestify

Property 'locals' does not exist on type 'Response'


I want to use res.locals to pass some data between middlewares but typescript says I can't. :(

I am using Restify by the way. Do I need a plugin? If not, an alternative solution for passing data between middlewares would be appreciated.


Solution

  • ⚠️ If you use typescript, then you get an error, because locals is not part of Interface res.

    So, if you want to add your own property, for example in res, then you can make it like this: res['locals'] = { name: 'John doe' }.

    📤 Update: Add properties to restify res

    To declare locals properties to your restifyResponse, than you can use the code below:

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

    After you declare your own properties, An example locals in your Restify Response, so, now you can use locals in your restifyresponse.