1

Types for @hapi/joi seem to be outdated - some configuration params mentioned in the official documentation are missing in the types. So I am trying to augment the types.

node_modules/@types/hapi__joi/index.d.ts:

declare namespace Joi {
...
interface ErrorFormattingOptions {...}
...
interface Root {...}
}

declare const Joi: Joi.Root;
export = Joi;

One of my .ts source files:

import joi from '@hapi/joi'

declare global {
  namespace Joi {
    interface ErrorFormattingOptions {
      wrap?: {
        label?: string | false
        array?: string | false
      }
    }
  }
}

That doesn't work

type R = Joi.ErrorFormattingOptions['']//autocomplete show only my 'wrap'

What is the proper way of augmentation in this case? Thanks.

UPDATE: Looks like that's impossible. Module '@hapi/joi' has no named export ErrorFormattingOptions. And in Typescript one cannot augment what's not exported

vatosarmat
  • 1,090
  • 10
  • 22

1 Answers1

0

Closest I've come to extend augment joi is adding a new method

joi-objectid.d.ts

declare module 'joi-objectid' {
  import Joi from 'joi';

  function objectId(): Joi.AlternativesSchema;

  function joiObjectId(joi: Joi.Root, message?: string): typeof objectId;

  export = joiObjectId;
}

joi.d.ts

import joiObjectId from 'joi-objectid';

declare module 'joi' {
  interface Root {
    objectId(): joiObjectId;
  }
}

Similarly you could try augment ErrorFormattingOptions interface directly on joi module and not overriding the one loaded onto the global scope I guess.

PrivateOmega
  • 2,509
  • 1
  • 17
  • 27