TypeScript中的部分环境内部模块声明?

时间:2014-08-22 12:07:08

标签: javascript d3.js typescript

我正在使用Jason Davies' TypeScript项目中的wordcloud,它基于D3库构建。具体来说,它定义了一个新的函数cloud(),它定义为从D3.layout扩展的成员,如 D3.layout.cloud()。要为TypeScript模块指定它,我需要在环境模块声明中定义它,我们在Boris Yankov的TypeScript declarations的确定列表中有。

有没有办法创建扩展现有声明的部分模块声明(.d.ts)文件?或者我是否必须编辑自己的标准声明文件副本(d3.d.ts)并在其中插入新成员?

PS感谢Davies先生和Yankov先生的努力,以这种方式学习TypeScript更加有趣:)。

1 个答案:

答案 0 :(得分:4)

只需添加一个新界面(它们都被视为部分并由TypeScript合并)。

e.g。扩展jQuery的定义我添加了这样的界面:

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

要扩展String我这样做了:

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

要扩展Date对象(以支持部分“mm / yy”日期),我这样做了:

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