2012-10-04 82 views
1

讓我們假設我定義的模塊,並且我有一些函數的定義是這樣的:我如何重構類型註釋?

export function bodyParser(options?:any): 
    (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; 
export function errorHandler(opts?:any): 
    (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; 
export function methodOverride(): 
    (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; 
export function favicon(url: string, opts?): 
    (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; 
export function logger(type: string, opts?): 
    (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; 

我反覆使用這個返回類型註釋:

(req: ExpressServerRequest, res: ExpressServerResponse, next) =>void 

有沒有一種方法來定義該類型,分配一個名稱,只引用名稱,而不是重複相同的事情?

回答

6

是的!您可以使用接口與調用簽名:

interface MyCallSignature { 
    (req: ExpressServerRequest, res: ExpressServerResponse, next): void; 
} 

現在你可以使用它像:

export function bodyParser(options?: any): MyCallSignature