TypeScript JQuery Promise parameter mismatch

I am using TypeScript 0.8.2 and the latest jQuery 1.9.d.ts definitions from https://github.com/borisyankov/DefinitelyTyped/tree/master/jquery

To isolate the problem, I have a simple TypeScript class definition that tries to make a single call to $ .ajax using the syntax .when () and .then (). Here is my code:

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

In both of these test cases, I get a compile-time error that reads:

The supplied parameters do not correspond to any signature of the target call, and red squiggly - according to the first parameter of the .when () method. He screenshot:

TypeScript jQuery Deferred Compile Problem

As far as I can tell, the .when () method in the .d.ts file has an overload of .when (options: any), and .ajax is defined as a JQueryXHR type that implements the JQueryPromise interface.

In theory, this should work fine, as it reflects the jQuery documentation for .when () http://api.jquery.com/jQuery.when/

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

So what am I missing? Did I spot something wrong?

+4
source share
1 answer

There may be a small error in a jQuery file type. The following works:

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

This is because a type file expects you to pass both a success handler and a failure handler. I checked the documentation and updated the definition to show that the failure handler is optional.

Update

I introduced the following change to determine jQuery type:

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

Changed to

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

All Articles