2017-05-09 62 views
0

我試圖覆蓋Response類型的節點快速send方法。 的方法是Send類型被定義爲這樣的:使用混合接口的Typescript覆蓋快速發送功能

interface Send { 
    (status: number, body?: any): Response; 
    (body?: any): Response; 
} 

我的目標是增加響應的本地記錄與寄快件,但我不能讓這種類型的實現,甚至作爲一個功能就像在其他問題like this中解釋的那樣。

回答

0

這個接口描述了兩個簽名的功能,你可以像這樣實現:

const fn: Send = (first?: any, second?: any) => { 
    if (first && second) { 
     // deal with (status: number, body: any) 
    } else if (first) { 
     // deal with (status: number) or (body: any) 
    } else { 
     // deal with() 
    } 

    return new Response(); 
} 
+0

雖然技術上是正確的,則'=(...)=>'格式結合'this'當前類在我的設置中,使其無法使用快速發送功能。相反,我找到並使用了[express-mung](https://www.npmjs.com/package/express-mung),實現了我最初想做的事情。 – nonelse