【问题标题】:Custom TypeScript definitions file not picked up by compiler?编译器未拾取自定义 TypeScript 定义文件?
【发布时间】:2013-09-10 17:38:40
【问题描述】:

我想在使用 TypeScript 的 Web 应用程序中使用 jQuery 工具库中的 Scrollable UI tool。由于DefinitelyTyped GitHub repository 上没有相应的定义文件,我创建了自己的:

/// <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 编译器拾取?

【问题讨论】:

    标签: typescript jquery-tools


    【解决方案1】:

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

    /// <reference path="scrollable.d.ts" />
    
    $('#id').scrollable({ vertical: true });
    

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

    【讨论】:

    • 这成功了;我忘记打开主要的 TypeScript 文件并将引用路径添加到其中的大引用列表中。我添加了参考路径,现在一切都按预期工作。谢谢!
    猜你喜欢
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2023-03-31
    相关资源
    最近更新 更多