Typescript/Hapi:Property';示例';不存在于类型';插件属性';

Typescript/Hapi:Property';示例';不存在于类型';插件属性';,typescript,typescript-typings,hapijs,hapi,Typescript,Typescript Typings,Hapijs,Hapi,Ii我有一个hapi插件,如下所示: exports.plugin = { name: 'example', register: function (server, options) { server.expose('key', 'value'); console.log(server.plugins.example.key); // 'value' } }; 我可以看到插件在路由处理程序中公开,但是当我尝试使用以下方法访问

Ii我有一个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;
我有一个typescript错误,类型“PluginProperties”上不存在属性“example”。


如何将此插件和其他插件添加到hapi类型?

您可以通过在types/hapi/index.d.ts中扩展hapi模块来定义插件属性字段和类型:


通过在types/hapi/index.d.ts中扩展hapi模块,可以定义pluginProperty字段和类型:

通过从tsconfig中删除“strict”:true,这将删除错误通过从tsconfig中删除“strict”:true,这将删除错误
declare module 'hapi' {
  export interface PluginProperties {
    [key: string]: any; // TODO define
  }
}