4

我正在使用来自https://github.com/borisyankov/DefinitelyTyped/tree/master/jquery的 TypeScript 0.8.2 和最新的 JQuery 1.9 .d.ts 定义

为了隔离问题,我有一个简单的 TypeScript 类定义,它尝试使用 .when() 和 .then() 语法进行单个 $.ajax 调用。这是我的代码:

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

module Demo {

    // Class
    export class TestDeferred {
        // Constructor
        constructor() {

            $.when(this.testAjaxCall()).then((data, status, jqXHR: JQueryXHR) => {
                alert(jqXHR.statusText);
            });

            $.when($.ajax("test.htm")).then(() => {
                console.log("yay");
            });
        }

        testAjaxCall() {
            return $.ajax("Test.aspx");
        }
    }
}

在这两个测试用例中,我得到一个编译时错误,上面写着:

提供的参数与调用目标的任何签名都不匹配,红色波浪线位于 .when() 方法的第一个参数上。他是截图:

TypeScript JQuery 延迟编译问题

据我所知,.d.ts 文件中的 .when() 方法具有 .when(options: any) 的重载,并且 .ajax 被定义为实现 JQueryPromise 接口的 JQueryXHR 类型。

从理论上讲,这应该可以正常工作,因为它反映了 .when() http://api.jquery.com/jQuery.when/的 jQuery 文档

$.when( $.ajax("test.aspx") ).then(function(data, textStatus, jqXHR){
     alert( jqXHR.status ); // alerts 200
});

那么我错过了什么?我定义错了吗?

4

1 回答 1

8

jQuery 的类型文件中可能存在细微的错误。以下作品:

$.when( $.ajax("test.aspx") ).then(function(data, textStatus, jqXHR){
     alert( jqXHR.status );
}, null);

这是因为类型文件希望您同时传递成功处理程序和失败处理程序。我将检查文档并更新定义以显示故障处理程序是可选的。

更新

我已向 jQuery 类型定义提交了以下更改:

then(doneCallbacks: any, failCallbacks: any, progressCallbacks?: any): JQueryPromise;

变成

then(doneCallbacks: any, failCallbacks?: any, progressCallbacks?: any): JQueryPromise;
于 2013-01-22T21:37:27.703 回答