1

我想在使用 TypeScript 的 Web 应用程序中使用 jQuery Tools 库中的Scrollable UI 工具不幸的是, DefinitelyTyped GitHub 存储库上没有相应的定义文件,我创建了自己的:

/// <reference path="./jquery-1.8.d.ts"/>

declare module JQueryTools {

    interface ScrollableOptions {
        clonedClass?: string;
        disabledClass?: string;
        easing?: string;
        items?: string;
        keyboard?: any; // boolean or string
        circular?: any // boolean
        next?: string;
        prev?: string;
        speed?: number;
        vertical?: any; // boolean
    }

    interface ScrollableUIParams {

    }

    interface ScrollableEvent {
        (event: Event, ui: ScrollableUIParams): void;
    }

    interface ScrollableEvents {
        onBeforeSeek?: ScrollableEvent;
        onSeek?: ScrollableEvent;
        onAddItem?: ScrollableEvent;
    }

    interface Scrollable extends ScrollableOptions, ScrollableEvents {
    }
}

interface JQuery {
    scrollable(): JQuery;
    scrollable(methodName: string): JQuery;
    scrollable(options: JQueryTools.ScrollableOptions): JQuery;
    scrollable(optionLiteral: string, optionName: string): any;
    scrollable(optionLiteral: string, options: JQueryTools.ScrollableOptions): any;
    scrollable(optionLiteral: string, optionName: string, optionValue: any): JQuery;
}

在我的 .ts 文件中,我有以下调用:

$(".scrollableWrapper").scrollable({vertical: true});

在编译时,我收到以下错误:

error TS2094: The property 'scrollable' does not exist on value of type 'JQuery'.

如果我从定义文件中删除所有代码并将其粘贴到 jquery-1.8.d.ts 文件中,一切似乎都可以正常工作,没有编译错误。那么,我的问题是:为什么我的定制 d.ts 文件没有被 typescript 编译器拾取?

4

1 回答 1

1

你的定义对我来说看起来不错,所以我从绝对类型中获取了 jQuery.d.ts 并将你的代码添加到 scrollable.d.ts 并且一切正常......只要我引用了你的定义......

/// <reference path="scrollable.d.ts" />

$('#id').scrollable({ vertical: true });

这让我觉得您可能没有引用您的定义文件(我将其称为 scrollable.d.ts,但您可能将其称为 jquery.scrollable.d.ts 或类似名称)。

于 2013-09-10T20:11:05.133 回答