hapijs / hapi-pino

🌲 Hapi plugin for the Pino logger
MIT License
148 stars 61 forks source link

Property 'logger' does not exist on type 'Request'. #172

Closed johnboulder closed 2 years ago

johnboulder commented 2 years ago

I could very well be wrong on this, but it seems like the module augmentation done on the Request object is not implemented properly.

I found myself on a journey learning about Module Augmentation recently that was sparked by what you guys are doing in this project. I noticed when trying to use the logger decorator in hapi plugins written in typescript, my IDE was failing to recognize the property was added on the Request object.

Screen Shot 2022-08-18 at 9 07 55 AM

Through testing, I found that the logger worked as expected, so this just meant that there was something wrong with the type resolution.

While working with writing my own decorators in typescript, and making sure the Module Augmentation got picked up correctly, I stumbled across this on stack overflow, and realized why the augmented type wasn't being resolved when I tried to use the one declared here in hapi-pino. So I figured I'd share!

Also, I never used the server.logger, decorator, but for all I know it could be in the same boat.

TLDR

The module augmentation on the Request object, declared in this project's types, needs to be adjusted in order for it to be resolved correctly in typescript projects.

This

declare module '@hapi/hapi' {
  interface Request {
    logger: pino.Logger;
  }
}

Should look like this

declare module '@hapi/hapi' {
  interface Request extends Podium {
    logger: pino.Logger;
  }
}
mcollina commented 2 years ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

jonathansamines commented 2 years ago

I tried to reproduce this by adding some tests to the type definitions at #174. The existing declarations seems to work just fine. The test suit uses:

"@types/hapi__hapi": "^20.0.10",