如何扩展Hapi类型[Typescript]

时间:2017-07-27 12:17:20

标签: typescript hapi.js

我正在尝试为存在的hapi定义添加新类型... 我想添加seneca类型......这是示例

interface SenecaMethods {
    act: any;
    add: any;
}

interface HapiServer extends Hapi.Server {
    info: any;
    seneca: SenecaMethods;
}

const server: HapiServer = new Hapi.Server();
  

返回错误服务器无法分配给HapiServer,属性   类型服务器中缺少'seneca'。

如何将seneca方法添加到Hapi.Server?我可以将seneca作为可选项,但我不希望它是可选的....

1 个答案:

答案 0 :(得分:3)

您有几个选择:

演员:

const server: HapiServer = new Hapi.Server() as HapiServer;

//server.seneca === undefined

<强>联合

const server: HapiServer = Object.assign(new Hapi.Server(),
    { seneca: { act: "foo", add: "bar" } }
  )

<强>延伸

class MyHapiServer extends Hapi.Server {
  info: any;
  seneca: SenecaMethods;

  constructor(){
    super();
    this.seneca = {
      act:"foo", 
      add:"bar",
    }
  }
}

const server: MyHapiServer = new MyHapiServer();