2

I'm struggling to understand the "correct" way to handle types in Hapi with @types/hapi.

A minimal example:

server.route({
  method: 'POST',
  path: '/example',
  options: {
    pre: [{
      assign: 'example',
      method: (req) => {
        return { msg: `Mr. ${req.payload.name} Example` };
      },
    }],
  },
  handler: (req) => {
    return {
      success: true,
      info: req.pre.example.msg,
    };
  },
});

My question is simply how do I get across the correct typings for req.payload and req.pre?

I can cheat the payload with type assertion but that seems unideal. I can't seem to even do that for pre. I'll just get Property '[key]' does not exist on type 'object'..

For what it's worth I'm using io-ts to validate the payload in my actual code, but validation appears to make no difference to the types.

Help appreciated, cheers!

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
Sam A. Horvath-Hunt
  • 931
  • 1
  • 7
  • 20
  • Answered a similar question, maybe you can use a similar approach ? https://stackoverflow.com/questions/48690619/how-can-i-augment-a-property-within-a-third-party-typescript-interface-defined-a/48692419#48692419 – Titian Cernicova-Dragomir May 08 '18 at 10:35
  • @TitianCernicova-Dragomir I don't think so. In this case I want to be able to specify the types of each server route individually i.e. whereas this example's payload contains `name`, the others will all contain wholly different properties. Unless it's possible to make it generic? – Sam A. Horvath-Hunt May 08 '18 at 10:42

0 Answers0