3

I'm in the process of building a declaration source file for KeyboardJS. The API is referenced from a static object (KeyboardJS) instantiated when the JavaScript file is loaded. A few of the methods of the API are nested under other methods. For example:

KeyboardJS.clear(keyCombo);

and

KeyboardJS.clear.key(key);

Here is my interface definition:

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; 
}

The TypeScript plugin for Visual Studio 2012 is generating and error on the clear.key line, recommending a semicolon for where the period is. Has anyone built a definition file with a similar scenario? Thanks!

2
  • 1
    Once done, you are more than welcome to submit the definition file to github.com/borisyankov/DefinitelyTyped Commented Jan 17, 2013 at 1:26
  • 1
    Thanks Boris. Honestly, that was my plan all along. Great job with DefinitelyTyped! Commented Jan 17, 2013 at 16:20

2 Answers 2

9

You can declare call signatures on types like this:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly. Also you can declare the whole thing as a ' interface KeyboardJsClear { }'
2

Genius comment from Ryan. Using the same pattern as the jQuery definition you could do:

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('');

2 Comments

Think about the jquery definitions. $() and $.ajax() etc
Thanks guys, this was a big help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.