iennacca
iennacca

Reputation: 33

Partial ambient internal module declarations in TypeScript?

I'm using Jason Davies' wordcloud in a TypeScript project, which builds on the D3 library. Specifically, it defines a new function cloud() that is defined as a member extended from D3.layout, as in D3.layout.cloud(). To specify this for TypeScript modules, I'd need it defined in an ambient module declaration, which we do have in Boris Yankov's definitive list of TypeScript declarations.

Is there a way to create a partial module declaration (.d.ts) file that extends an already existing declaration? Or would I have to edit my own copy of the standard declaration file (d3.d.ts) and insert the new member therein?

PS Thanks to Mr. Davies and Mr. Yankov's efforts, it's been more fun learning TypeScript this way :).

Upvotes: 3

Views: 703

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93631

Just add a new interface (they are all treated as partials and merged by TypeScript).

e.g. to extend definitions of jQuery I added an interface like this:

// Declare our additions to JQuery
interface JQuery
{
    isDescendentOf(element: any);
    attrString(): string;
    cssInt(any): number;
    cssFloat(any): number;
    reverse(): JQuery;

    // Compensate for missing (valid) trigger overload
    trigger(event: JQueryEventObject, ...extraParameters: any[]): JQuery;
}

To extend String I did this:

interface String
{
    startsWith(text: string): boolean;
}

To extend the Date object (to support partial "mm/yy" dates) I did this:

// Extend the date class, so we can include the partial flag
interface Date
{
    isPartial: boolean;
    isValid: boolean;
    partialDate(): string;
}

Upvotes: 4

Related Questions