使用jQuery UI Widgets的Typescript - 实现调用签名

par*_*ent 5 javascript jquery-ui typescript

我在我的网站(登录对话框,注册对话框,联系人对话框等)中有很多对话框,我试图使用Typescript进行建模.

jquery-ui d.ts文件定义了一个Dialog接口:

interface Dialog extends Widget, DialogOptions, DialogEvents{  }
Run Code Online (Sandbox Code Playgroud)

一切都在DialogOptionsDialogEvents是可选的,所以有没有问题,但widget界面定义,我不理解,有人称之为签名:

interface Widget {
    (methodName: string): JQuery;
    (options: WidgetOptions): JQuery;
    (options: AccordionOptions): JQuery;
    (optionLiteral: string, optionName: string): any;
    (optionLiteral: string, options: WidgetOptions): any;
    (optionLiteral: string, optionName: string, optionValue: any): JQuery;

    (name: string, prototype: any): JQuery;
    (name: string, base: Function, prototype: any): JQuery;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在类中实现此接口时:

class LoginDialog implements Dialog { 

}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨:

Class 'LoginDialog' declares interface 'Dialog' but does not implement it: Type 'Dialog' requires a call signature, but Type 'LoginDialog' lacks one.
Run Code Online (Sandbox Code Playgroud)

我不明白如何实现这些"呼叫签名"或他们应该做什么,事实上,我甚至不了解呼叫签名是什么.

编辑:

我发现呼叫签名的一种有效用例是回调输入:

interface ICallback{
    (param: string) : void;
}

function someMethod(callback: ICallback){
    callback('a string'); //Good
    callback(5);         //Bad 
}
Run Code Online (Sandbox Code Playgroud)

但是,问题仍然存在,因为我找不到多个呼叫签名的用例,仍然不明白如何实现jquery-ui.d.ts Dialog接口

Fen*_*ton 5

如果您使用的是jQuery UI,则不需要实现任何这些接口,因为它们已经在jQuery UI中实现.这些接口只是为您提供类型信息,用于通过静态类型调用现有的jQuery UI函数.

$( "#dialog" ).dialog();
Run Code Online (Sandbox Code Playgroud)

例如,没有jquery-ui.d.ts类型系统就不会知道该dialog()功能.