Typescript/Hapi: свойство «пример» не существует для типа «PluginProperties»

У меня есть плагин hapi, такой как приведенный ниже:

exports.plugin = {
    name: 'example',
    register: function (server, options) {

        server.expose('key', 'value');

        console.log(server.plugins.example.key);      // 'value'
    }
};

Я вижу, что плагин отображается в обработчике маршрута, однако, когда я пытаюсь получить доступ к этому значению, используя:

async handler(request: Request, h: ResponseToolkit) {
      const value = request.server.plugins.example.key;

У меня ошибка машинописи Property 'example' does not exist on type 'PluginProperties'.

Как добавить этот и другие плагины в тип хапи?


person Paul    schedule 03.08.2020    source источник
comment
Удалив `strict: true из tsconfig, это устранило ошибку   -  person Paul    schedule 05.08.2020


Ответы (1)


Вы можете определить поля и типы PluginProperties, расширив модуль hapi в types/hapi/index.d.ts:

declare module 'hapi' {
  export interface PluginProperties {
    [key: string]: any; // TODO define
  }
}
person Andrej Leitner    schedule 19.08.2020