如何实例化这种接口(d.ts
)?
export interface IReply {
<T>(err: Error, result?: string | number | boolean | Buffer | stream.Stream | IPromise<T> | T, credentialData?: any): IBoom;
<T>(result: string | number | boolean | Buffer | stream.Stream | IPromise<T> | T): Response;
continue(credentialData?: any): void;
//...
}
目标是调用此函数(hapi
处理函数):
static healthcheck: ISessionHandler = (request: Request, reply: IReply): void => {
reply({healthy: true});
};
以这样的方式:
HealthcheckController.healthcheck(request, (reply) => {
console.log('reply : ' + reply);
});
如果我删除了打字要求,这一切都有效 - &gt;有效的JavaScript。但似乎无法让它在TypeScript中运行。它在tsc上失败了。
我似乎无法实例化一个允许我使用TypeScript成功调用reply
函数的healthcheck
变量。
答案 0 :(得分:1)
你可以像下面的(简单的)样本那样做:
export interface IReply
{
(result: any): string;
continue(credentialData?: any): void;
}
let healthCheck = (reply: IReply): void =>
{
reply({healthy: true});
};
var f = <IReply>((reply: any) => { console.log("reply: ", reply); return reply; });
f.continue = (credentialData?: any) => { console.log("continue: ", credentialData)};
healthCheck(f);