CodingIntrigue
CodingIntrigue

Reputation: 78525

Adding overload for static method in TypeScript definition

I'm writing a TypeScript definition file for a jQuery library which has the following syntax:

$.library({ /* options object */ }); // Returns an Object
$.library.methodOne(); // Void
$.library.methodTwo(); // Void

So I tried writing the following interface:

interface JQueryStatic {
    library: StaticLibraryMethods; // Defines the static methods
    library(options:LibraryOptions): Object; // Defines the options overflow
}
interface StaticLibraryMethods {
    methodOne(): void;
    methodTwo(): void;
}

But I get an error in JQueryStatic:

Duplicate identifier "library"

Is there any way to write a definition for this kind of syntax without modifying library itself?

Upvotes: 0

Views: 803

Answers (1)

David Sherret
David Sherret

Reputation: 106640

You have to define it like so:

interface JQueryStatic {
    (options: LibraryOptions): Object;
    library: StaticLibraryMethods; // Defines the static methods
}

interface StaticLibraryMethods {
    methodOne(): void;
    methodTwo(): void;
}

Or you could do this:

interface JQueryStatic {
    library: MyLibrary;
}

interface MyLibrary {
    (options: LibraryOptions): Object;
    methodOne(): void;
    methodTwo(): void;
}

Upvotes: 1

Related Questions