3

我正在为KeyboardJS构建声明源文件。该 API 是从加载 JavaScript 文件时实例化的静态对象 (KeyboardJS) 引用的。API 的一些方法嵌套在其他方法下。例如:

KeyboardJS.clear(keyCombo);

KeyboardJS.clear.key(key);

这是我的接口定义:

interface KeyboardJSStatic {
    enable(): void;
    disable(): void;
    activeKeys(): string[];
    on(keyCombo:string, onDownCallback?: (keyEvent: Event, keysPressed: string[], keyCombo: string) => {}, onUpCallback?: (keyEvent: Event, keysPressed: string[], keyCombo: string) => {}): KeyboardJSBinding;
    clear(keyCombo: string): void;
    clear.key(keyName: string): void;
    locale(localeName: string): KeyboardJSLocale;
    locale.register(localeName: string, localeDefinition: KeyboardJSLocale): void;
    macro(keyCombo:string , keyNames: string[]): void;
    macro.remove(keyCombo: string): void;
    key: KeyboardJSKey;
    combo: KeyboardJSCombo; 
}

Visual Studio 2012 的 TypeScript 插件在 clear.key 行上生成并出错,建议在句点所在的位置使用分号。有没有人用类似的场景建立一个定义文件?谢谢!

4

2 回答 2

9

您可以在如下类型上声明调用签名:

interface KeyboardJSStatic {
    // ...
    clear: {
        (keyCombo: string): void; // Call signature
        key(keyName: string): void; // Method
    };
    // ...
}

var n: KeyboardJSStatic;
n.clear('a'); // OK
n.clear.key('b'); // OK
于 2013-01-17T00:05:30.287 回答
2

来自瑞安的天才评论。使用与 jQuery 定义相同的模式,您可以执行以下操作:

interface KeyboardJSClear {
    (keyCombo: string) : void;
    key(keyName: string): void;
}

interface KeyboardJSLocale {
    (localeName: string) : KeyboardJSLocale;
    register(localeName: string, localeDefinition: KeyboardJSLocale): void;
}

interface KeyboardJSMacro {
    (keyCombo:string , keyNames: string[]): void;
    remove(keyCombo: string): void;
}

interface KeyboardJSStatic {
    enable(): void;
    disable(): void;
    activeKeys(): string[];
    on(keyCombo:string, onDownCallback?: (keyEvent: Event, keysPressed: string[], keyCombo: string) => {}, onUpCallback?: (keyEvent: Event, keysPressed: string[], keyCombo: string) => {}): KeyboardJSBinding;
    clear: KeyboardJSClear;
    locale: KeyboardJSLocale;
    macro: KeyboardJSMacro;
    key: KeyboardJSKey;
    combo: KeyboardJSCombo; 
}

declare var KeyboardJS: KeyboardJSStatic;

KeyboardJS.clear('');
KeyboardJS.clear.key('');
于 2013-01-16T23:12:11.003 回答