2017-07-27 60 views
0

我嘗試新類型添加到existsing高致病性禽流感的定義...... 我想補充塞內卡類型...這裏是例子如何延長哈啤類型[打字稿]

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?我可以把塞涅卡作爲可選的,但我不希望它是可選....

+0

看看聲明部分(TypeScript doc):https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html –

+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();