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

有没有办法定义那个类型,给它一个名字,然后只引用这个名字而不是重复同样的事情?

4

1 回答 1

6

是的!您可以使用带有调用签名的接口:

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

现在你可以像这样使用它:

export function bodyParser(options?: any): MyCallSignature
于 2012-10-04T22:56:39.107 回答