From 725f7676c5c38639fc4767792975caae01b89ff5 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 09:14:00 -0400 Subject: [PATCH 01/35] [jquery] Add more tests for JQueryStatic. --- types/jquery/jquery-tests.ts | 101 +++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 5 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 7a05a5630a..88cf87c95c 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -3531,6 +3531,58 @@ function JQueryStatic() { $(); } + function Event() { + // #ExpectType Event + $.Event; + } + + function cssHooks() { + // #ExpectType PlainObject> + $.cssHooks; + } + + function cssNumber() { + // #ExpectType PlainObject + $.cssNumber; + } + + function fn() { + // #ExpectType JQuery + $.fn; + } + + function fx() { + function interval() { + // #ExpectType JQuery + $.fx.interval; + } + + function off() { + // #ExpectType boolean + $.fx.off; + } + + function step() { + // #ExpectType PlainObject> + $.fx.step; + } + } + + function ready() { + // #ExpectType Thenable> + $.ready; + } + + function support() { + // #ExpectType PlainObject + $.support; + } + + function valHooks() { + // #ExpectType PlainObject> + $.valHooks; + } + function Callbacks() { // #ExpectType Callbacks $.Callbacks('once'); @@ -3539,11 +3591,50 @@ function JQueryStatic() { $.Callbacks(); } - function Event() { - function constructor() { - const e = $.Event('click'); - e.stopPropagation(); - } + function Deferred() { + // #ExpectType Deferred + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + + // #ExpectType Deferred + $.Deferred(); + + // #ExpectType Deferred + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + + // #ExpectType Deferred + $.Deferred(); + + // #ExpectType Deferred + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + + // #ExpectType Deferred + $.Deferred(); + + // #ExpectType Deferred + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + + // #ExpectType Deferred + $.Deferred(); } function ajax() { From 7ddee109a1fdcd06635215151fcafa143f858845 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 09:46:10 -0400 Subject: [PATCH 02/35] [jquery] Updated AjaxSettings. --- types/jquery/index.d.ts | 276 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 271 insertions(+), 5 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index ce32477d98..eb41fd1845 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3482,7 +3482,7 @@ declare namespace JQuery { * XMLHttpRequest value can be changed here. Values in the headers setting can also be overwritten from * within the beforeSend function. */ - headers?: PlainObject; + headers?: PlainObject; /** * Allow the request to be successful only if the response has changed since the last request. This is * done by checking the Last-Modified header. Default value is false, ignoring the header. In jQuery @@ -3578,6 +3578,7 @@ declare namespace JQuery { * A username to be used with XMLHttpRequest in response to an HTTP access authentication request. */ username?: string; + // ActiveXObject requires "lib": ["scripthost"] which consumers would also require /** * Callback for creating the XMLHttpRequest object. Defaults to the ActiveXObject when available (IE), * the XMLHttpRequest otherwise. Override to provide your own implementation for XMLHttpRequest or @@ -3591,12 +3592,14 @@ declare namespace JQuery { * requests requiring it would ignore this flag. For this reason, we recommend using jQuery 1.5.1+ * should you require the use of it. */ - xhrFields?: PlainObject; + xhrFields?: XHRFields; } - // Status codes not listed require type annotations when defining the callback type StatusCodeCallbacks = { + // region Success Status Codes + // jQuery treats 2xx and 304 status codes as a success + 200?: SuccessCallback; 201?: SuccessCallback; 202?: SuccessCallback; @@ -3699,7 +3702,10 @@ declare namespace JQuery { 299?: SuccessCallback; 304?: SuccessCallback; - // Standard 3xx, 4xx, and 5xx status codes that are considered an error + // endregion + + // region Error Status Codes + 300?: ErrorCallback; 301?: ErrorCallback; 302?: ErrorCallback; @@ -3708,6 +3714,97 @@ declare namespace JQuery { 306?: ErrorCallback; 307?: ErrorCallback; 308?: ErrorCallback; + 309?: ErrorCallback; + 310?: ErrorCallback; + 311?: ErrorCallback; + 312?: ErrorCallback; + 313?: ErrorCallback; + 314?: ErrorCallback; + 315?: ErrorCallback; + 316?: ErrorCallback; + 317?: ErrorCallback; + 318?: ErrorCallback; + 319?: ErrorCallback; + 320?: ErrorCallback; + 321?: ErrorCallback; + 322?: ErrorCallback; + 323?: ErrorCallback; + 324?: ErrorCallback; + 325?: ErrorCallback; + 326?: ErrorCallback; + 327?: ErrorCallback; + 328?: ErrorCallback; + 329?: ErrorCallback; + 330?: ErrorCallback; + 331?: ErrorCallback; + 332?: ErrorCallback; + 333?: ErrorCallback; + 334?: ErrorCallback; + 335?: ErrorCallback; + 336?: ErrorCallback; + 337?: ErrorCallback; + 338?: ErrorCallback; + 339?: ErrorCallback; + 340?: ErrorCallback; + 341?: ErrorCallback; + 342?: ErrorCallback; + 343?: ErrorCallback; + 344?: ErrorCallback; + 345?: ErrorCallback; + 346?: ErrorCallback; + 347?: ErrorCallback; + 348?: ErrorCallback; + 349?: ErrorCallback; + 350?: ErrorCallback; + 351?: ErrorCallback; + 352?: ErrorCallback; + 353?: ErrorCallback; + 354?: ErrorCallback; + 355?: ErrorCallback; + 356?: ErrorCallback; + 357?: ErrorCallback; + 358?: ErrorCallback; + 359?: ErrorCallback; + 360?: ErrorCallback; + 361?: ErrorCallback; + 362?: ErrorCallback; + 363?: ErrorCallback; + 364?: ErrorCallback; + 365?: ErrorCallback; + 366?: ErrorCallback; + 367?: ErrorCallback; + 368?: ErrorCallback; + 369?: ErrorCallback; + 370?: ErrorCallback; + 371?: ErrorCallback; + 372?: ErrorCallback; + 373?: ErrorCallback; + 374?: ErrorCallback; + 375?: ErrorCallback; + 376?: ErrorCallback; + 377?: ErrorCallback; + 378?: ErrorCallback; + 379?: ErrorCallback; + 380?: ErrorCallback; + 381?: ErrorCallback; + 382?: ErrorCallback; + 383?: ErrorCallback; + 384?: ErrorCallback; + 385?: ErrorCallback; + 386?: ErrorCallback; + 387?: ErrorCallback; + 388?: ErrorCallback; + 389?: ErrorCallback; + 390?: ErrorCallback; + 391?: ErrorCallback; + 392?: ErrorCallback; + 393?: ErrorCallback; + 394?: ErrorCallback; + 395?: ErrorCallback; + 396?: ErrorCallback; + 397?: ErrorCallback; + 398?: ErrorCallback; + 399?: ErrorCallback; 400?: ErrorCallback; 401?: ErrorCallback; 402?: ErrorCallback; @@ -3727,15 +3824,87 @@ declare namespace JQuery { 416?: ErrorCallback; 417?: ErrorCallback; 418?: ErrorCallback; + 419?: ErrorCallback; + 420?: ErrorCallback; 421?: ErrorCallback; 422?: ErrorCallback; 423?: ErrorCallback; 424?: ErrorCallback; + 425?: ErrorCallback; 426?: ErrorCallback; + 427?: ErrorCallback; 428?: ErrorCallback; 429?: ErrorCallback; + 430?: ErrorCallback; 431?: ErrorCallback; + 432?: ErrorCallback; + 433?: ErrorCallback; + 434?: ErrorCallback; + 435?: ErrorCallback; + 436?: ErrorCallback; + 437?: ErrorCallback; + 438?: ErrorCallback; + 439?: ErrorCallback; + 440?: ErrorCallback; + 441?: ErrorCallback; + 442?: ErrorCallback; + 443?: ErrorCallback; + 444?: ErrorCallback; + 445?: ErrorCallback; + 446?: ErrorCallback; + 447?: ErrorCallback; + 448?: ErrorCallback; + 449?: ErrorCallback; + 450?: ErrorCallback; 451?: ErrorCallback; + 452?: ErrorCallback; + 453?: ErrorCallback; + 454?: ErrorCallback; + 455?: ErrorCallback; + 456?: ErrorCallback; + 457?: ErrorCallback; + 458?: ErrorCallback; + 459?: ErrorCallback; + 460?: ErrorCallback; + 461?: ErrorCallback; + 462?: ErrorCallback; + 463?: ErrorCallback; + 464?: ErrorCallback; + 465?: ErrorCallback; + 466?: ErrorCallback; + 467?: ErrorCallback; + 468?: ErrorCallback; + 469?: ErrorCallback; + 470?: ErrorCallback; + 471?: ErrorCallback; + 472?: ErrorCallback; + 473?: ErrorCallback; + 474?: ErrorCallback; + 475?: ErrorCallback; + 476?: ErrorCallback; + 477?: ErrorCallback; + 478?: ErrorCallback; + 479?: ErrorCallback; + 480?: ErrorCallback; + 481?: ErrorCallback; + 482?: ErrorCallback; + 483?: ErrorCallback; + 484?: ErrorCallback; + 485?: ErrorCallback; + 486?: ErrorCallback; + 487?: ErrorCallback; + 488?: ErrorCallback; + 489?: ErrorCallback; + 490?: ErrorCallback; + 491?: ErrorCallback; + 492?: ErrorCallback; + 493?: ErrorCallback; + 494?: ErrorCallback; + 495?: ErrorCallback; + 496?: ErrorCallback; + 497?: ErrorCallback; + 498?: ErrorCallback; + 499?: ErrorCallback; 500?: ErrorCallback; 501?: ErrorCallback; 502?: ErrorCallback; @@ -3745,9 +3914,106 @@ declare namespace JQuery { 506?: ErrorCallback; 507?: ErrorCallback; 508?: ErrorCallback; + 509?: ErrorCallback; 510?: ErrorCallback; 511?: ErrorCallback; - } & { [index: number]: SuccessCallback | ErrorCallback; }; + 512?: ErrorCallback; + 513?: ErrorCallback; + 514?: ErrorCallback; + 515?: ErrorCallback; + 516?: ErrorCallback; + 517?: ErrorCallback; + 518?: ErrorCallback; + 519?: ErrorCallback; + 520?: ErrorCallback; + 521?: ErrorCallback; + 522?: ErrorCallback; + 523?: ErrorCallback; + 524?: ErrorCallback; + 525?: ErrorCallback; + 526?: ErrorCallback; + 527?: ErrorCallback; + 528?: ErrorCallback; + 529?: ErrorCallback; + 530?: ErrorCallback; + 531?: ErrorCallback; + 532?: ErrorCallback; + 533?: ErrorCallback; + 534?: ErrorCallback; + 535?: ErrorCallback; + 536?: ErrorCallback; + 537?: ErrorCallback; + 538?: ErrorCallback; + 539?: ErrorCallback; + 540?: ErrorCallback; + 541?: ErrorCallback; + 542?: ErrorCallback; + 543?: ErrorCallback; + 544?: ErrorCallback; + 545?: ErrorCallback; + 546?: ErrorCallback; + 547?: ErrorCallback; + 548?: ErrorCallback; + 549?: ErrorCallback; + 550?: ErrorCallback; + 551?: ErrorCallback; + 552?: ErrorCallback; + 553?: ErrorCallback; + 554?: ErrorCallback; + 555?: ErrorCallback; + 556?: ErrorCallback; + 557?: ErrorCallback; + 558?: ErrorCallback; + 559?: ErrorCallback; + 560?: ErrorCallback; + 561?: ErrorCallback; + 562?: ErrorCallback; + 563?: ErrorCallback; + 564?: ErrorCallback; + 565?: ErrorCallback; + 566?: ErrorCallback; + 567?: ErrorCallback; + 568?: ErrorCallback; + 569?: ErrorCallback; + 570?: ErrorCallback; + 571?: ErrorCallback; + 572?: ErrorCallback; + 573?: ErrorCallback; + 574?: ErrorCallback; + 575?: ErrorCallback; + 576?: ErrorCallback; + 577?: ErrorCallback; + 578?: ErrorCallback; + 579?: ErrorCallback; + 580?: ErrorCallback; + 581?: ErrorCallback; + 582?: ErrorCallback; + 583?: ErrorCallback; + 584?: ErrorCallback; + 585?: ErrorCallback; + 586?: ErrorCallback; + 587?: ErrorCallback; + 588?: ErrorCallback; + 589?: ErrorCallback; + 590?: ErrorCallback; + 591?: ErrorCallback; + 592?: ErrorCallback; + 593?: ErrorCallback; + 594?: ErrorCallback; + 595?: ErrorCallback; + 596?: ErrorCallback; + 597?: ErrorCallback; + 598?: ErrorCallback; + 599?: ErrorCallback; + + // endregion + } & { + // Status codes not listed require type annotations when defining the callback + [index: number]: SuccessCallback | ErrorCallback; + }; + + // Writable properties on XMLHttpRequest + interface XHRFields extends Partial> { } } interface Transport { From f17d785e96781b9d5126fdbdfaa547b2510d0790 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 11:32:48 -0400 Subject: [PATCH 03/35] [jquery] Fix jqXHR, Deferred, and Promise. --- types/jquery/index.d.ts | 154 ++++++++++---------- types/jquery/jquery-tests.ts | 264 +++++++++++++++++++++++++++++++++-- 2 files changed, 328 insertions(+), 90 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index eb41fd1845..45baaba789 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4030,10 +4030,10 @@ declare namespace JQuery { /** * @see {@link http://api.jquery.com/jquery.ajax/#jqXHR} */ - interface jqXHR extends Pick { - responseJSON: any; - statusCode(map: Ajax.StatusCodeCallbacks): void; + interface jqXHR extends Pick, + Partial> { + responseJSON?: any; /** * Add handlers to be called when the Deferred object is either resolved or rejected. @@ -4093,6 +4093,8 @@ declare namespace JQuery { */ pipe(doneFilter: ((data: TResolve, textStatus: Ajax.SuccessTextStatus, jqXHR: this) => UResolve | Thenable) | null, failFilter?: ((jqXHR: this, textStatus: Ajax.ErrorTextStatus, errorThrown: string) => UReject | Thenable) | null): Deferred; + // jQuery doesn't send progress notifications so progress callbacks will never be called. + // Leaving progress() in however allows for structural compatibility with JQuery.Promise. /** * Add handlers to be called when the Deferred object generates progress notifications. * @@ -4102,8 +4104,8 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.progress/} * @since 1.7 */ - progress(progressCallback: TypeOrArray, - ...progressCallbacks: Array>): this; + progress(progressCallback: TypeOrArray, + ...progressCallbacks: Array>): this; /** * Return a Deferred's Promise object. * @@ -4126,6 +4128,7 @@ declare namespace JQuery { * @since 1.7 */ state(): 'pending' | 'resolved' | 'rejected'; + statusCode(map: Ajax.StatusCodeCallbacks): void; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4151,10 +4154,6 @@ declare namespace JQuery { interface AlwaysCallback { (data_jqXHR: TResolve | jqXHR, textStatus: Ajax.TextStatus, jqXHR_errorThrown: jqXHR | string): void; } - - interface ProgressCallback { - (...values: any[]): void; - } } // endregion @@ -4266,17 +4265,7 @@ declare namespace JQuery { */ interface Thenable extends PromiseLike { } - interface Deferred { - /** - * Add handlers to be called when the Deferred object is either resolved or rejected. - * - * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. - * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. - * @see {@link https://api.jquery.com/deferred.always/} - * @since 1.6 - */ - always(alwaysCallback: TypeOrArray>, - ...alwaysCallbacks: Array>>): this; + interface Deferred extends Deferred.PromiseBase { /** * Add handlers to be called when the Deferred object is rejected. * @@ -4285,26 +4274,6 @@ declare namespace JQuery { * @since 3.0 */ catch(failFilter: (...reasons: TReject[]) => UResolve | Thenable): Deferred; - /** - * Add handlers to be called when the Deferred object is resolved. - * - * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. - * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.done/} - * @since 1.5 - */ - done(doneCallback: TypeOrArray>, - ...doneCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. - * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.fail/} - * @since 1.5 - */ - fail(failCallback: TypeOrArray>, - ...failCallbacks: Array>>): this; /** * Call the progressCallbacks on a Deferred object with the given args. * @@ -4338,32 +4307,6 @@ declare namespace JQuery { UNotify = TNotify>(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; - /** - * Add handlers to be called when the Deferred object generates progress notifications. - * - * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. - * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates - * progress notifications. - * @see {@link https://api.jquery.com/deferred.progress/} - * @since 1.7 - */ - progress(progressCallback: TypeOrArray>, - ...progressCallbacks: Array>>): this; - /** - * Return a Deferred's Promise object. - * - * @param target Object onto which the promise methods have to be attached - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(target: TTarget): JQuery.Promise & TTarget; - /** - * Return a Deferred's Promise object. - * - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(): JQuery.Promise; /** * Reject a Deferred object and call any failCallbacks with the given args. * @@ -4398,13 +4341,6 @@ declare namespace JQuery { * @since 1.5 */ resolveWith(context: object, ...args: TResolve[]): this; - /** - * Determine the current state of a Deferred object. - * - * @see {@link https://api.jquery.com/deferred.state/} - * @since 1.7 - */ - state(): 'pending' | 'resolved' | 'rejected'; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4437,6 +4373,73 @@ declare namespace JQuery { interface ProgressCallback { (...values: TNotify[]): void; } + + // Common interface for Deferred and Promise + interface PromiseBase { + /** + * Add handlers to be called when the Deferred object is either resolved or rejected. + * + * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. + * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. + * @see {@link https://api.jquery.com/deferred.always/} + * @since 1.6 + */ + always(alwaysCallback: TypeOrArray>, + ...alwaysCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is resolved. + * + * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. + * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.done/} + * @since 1.5 + */ + done(doneCallback: TypeOrArray>, + ...doneCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. + * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.fail/} + * @since 1.5 + */ + fail(failCallback: TypeOrArray>, + ...failCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object generates progress notifications. + * + * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. + * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates + * progress notifications. + * @see {@link https://api.jquery.com/deferred.progress/} + * @since 1.7 + */ + progress(progressCallback: TypeOrArray>, + ...progressCallbacks: Array>>): this; + /** + * Return a Deferred's Promise object. + * + * @param target Object onto which the promise methods have to be attached + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(target: TTarget): JQuery.Promise & TTarget; + /** + * Return a Deferred's Promise object. + * + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(): JQuery.Promise; + /** + * Determine the current state of a Deferred object. + * + * @see {@link https://api.jquery.com/deferred.state/} + * @since 1.7 + */ + state(): 'pending' | 'resolved' | 'rejected'; + } } /** @@ -4445,8 +4448,7 @@ declare namespace JQuery { * * @see {@link http://api.jquery.com/Types/#Promise} */ - interface Promise extends Pick, - 'always' | 'done' | 'fail' | 'progress' | 'promise' | 'state'> { + interface Promise extends Deferred.PromiseBase { /** * Add handlers to be called when the Deferred object is rejected. * diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 88cf87c95c..64b171bdfd 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4684,7 +4684,7 @@ function EffectsOptions() { }); } -function JQuery_Event() { +function _Event() { function mixin() { const e = $.Event('keydown', { mySpecialKeyCode: JQuery.Key.CapsLock, @@ -4695,9 +4695,256 @@ function JQuery_Event() { } function jqXHR() { - function catch_returnType() { + function always() { + // $ExpectType jqXHR + $.ajax('/echo/json').always((data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }, [(data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }], (data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').always((data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }, [(data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }]); + + // $ExpectType jqXHR + $.ajax('/echo/json').always([(data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }], (data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').always((data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').always([(data_jqXHR, textStatus, jqXHR_errorThrown) => { + // $ExpectType any + data_jqXHR; + // $ExpectType TextStatus + textStatus; + // $ExpectType string | jqXHR + jqXHR_errorThrown; + }]); + } + + function done() { + // $ExpectType jqXHR + $.ajax('/echo/json').done((data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, [(data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }], (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').done((data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }, [(data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }]); + + // $ExpectType jqXHR + $.ajax('/echo/json').done([(data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }], (data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').done((data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').done([(data, textStatus, jqXHR) => { + // $ExpectType any + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }]); + } + + function fail() { + // $ExpectType jqXHR + $.ajax('/echo/json').fail((jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }, [(jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }], (jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').fail((jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }, [(jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }]); + + // $ExpectType jqXHR + $.ajax('/echo/json').fail([(jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }], (jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').fail((jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }); + + // $ExpectType jqXHR + $.ajax('/echo/json').fail([(jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }]); + } + + function _catch() { // $ExpectType Deferred - $.ajax('/echo').catch(() => { }); + $.ajax('/echo').catch((jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }); } function catch_throw_returnType() { @@ -4726,15 +4973,4 @@ function jqXHR() { value; }); } - - function fail() { - $.ajax('/echo').fail((jqXHR, textStatus, errorThrown) => { - // $ExpectType jqXHR - jqXHR; - // $ExpectType ErrorTextStatus - textStatus; - // $ExpectType string - errorThrown; - }); - } } From 20fbc1f61b43be08d8f22920d813725dbc709459 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 11:35:18 -0400 Subject: [PATCH 04/35] [jquery] Improve Callbacks. Make Callbacks generic. Fix some method signatures. --- types/jquery/index.d.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 45baaba789..3d65135f5f 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2499,7 +2499,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.Callbacks/} * @since 1.7 */ - Callbacks(flags?: string): JQuery.Callbacks; + Callbacks(flags?: string): JQuery.Callbacks; /** * A factory function that returns a chainable utility object with methods to register multiple * callbacks into callback queues, invoke callback queues, and relay the success or failure state of @@ -4160,15 +4160,16 @@ declare namespace JQuery { // region Callbacks - interface Callbacks { + interface Callbacks { /** * Add a callback or a collection of callbacks to a callback list. * + * @param callback A function, or array of functions, that are to be added to the callback list. * @param callbacks A function, or array of functions, that are to be added to the callback list. * @see {@link https://api.jquery.com/callbacks.add/} * @since 1.7 */ - add(callbacks: TypeOrArray): this; + add(callback: TypeOrArray, ...callbacks: Array>): this; /** * Disable a callback list from doing anything more. * @@ -4206,7 +4207,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/callbacks.fireWith/} * @since 1.7 */ - fireWith(context?: object, args?: TypeOrArray): this; + fireWith(context: object, args?: ArrayLike): this; /** * Determine if the callbacks have already been called at least once. * @@ -4222,7 +4223,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/callbacks.has/} * @since 1.7 */ - has(callback?: Function): boolean; + has(callback?: T): boolean; /** * Lock a callback list in its current state. * @@ -4244,7 +4245,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/callbacks.remove/} * @since 1.7 */ - remove(callbacks: TypeOrArray): this; + remove(...callbacks: T[]): this; } // endregion @@ -4901,7 +4902,7 @@ declare namespace JQuery { // region Legacy types -interface JQueryCallback extends JQuery.Callbacks { } +interface JQueryCallback extends JQuery.Callbacks { } interface JQueryDeferred extends JQuery.Deferred { } interface JQueryEventObject extends JQuery.Event { } interface JQueryEventConstructor extends JQuery.Event { } From 9c7cc11356d9ecb755e3f16b469d90806acb0461 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 12:10:50 -0400 Subject: [PATCH 05/35] [jquery] Add tests for Callbacks. --- types/jquery/jquery-tests.ts | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 64b171bdfd..1fda6e2da8 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4620,6 +4620,86 @@ function AjaxSettings() { }); } +function Callbacks() { + function add() { + const callbacks = $.Callbacks(); + + // $ExpectType Callbacks + callbacks.add(() => { }, [() => { }], () => { }); + + // $ExpectType Callbacks + callbacks.add(() => { }, [() => { }]); + + // $ExpectType Callbacks + callbacks.add(() => { }, () => { }); + + // $ExpectType Callbacks + callbacks.add(() => { }); + + // $ExpectType Callbacks + callbacks.add([() => { }]); + } + + function disable() { + // $ExpectType Callbacks + $.Callbacks().disable(); + } + + function disabled() { + // $ExpectType boolean + $.Callbacks().disabled(); + } + + function empty() { + // $ExpectType Callbacks + $.Callbacks().empty(); + } + + function fire() { + // $ExpectType Callbacks + $.Callbacks().fire(1); + + // $ExpectType Callbacks + $.Callbacks().fire(); + } + + function fireWith() { + // $ExpectType Callbacks + $.Callbacks().fireWith(window, [1]); + + // $ExpectType Callbacks + $.Callbacks().fireWith(window); + } + + function fired() { + // $ExpectType boolean + $.Callbacks().fired(); + } + + function has() { + // $ExpectType boolean + $.Callbacks().has(() => { }); + + // $ExpectType boolean + $.Callbacks().has(); + } + + function lock() { + // $ExpectType Callbacks + $.Callbacks().lock(); + } + + function locked() { + // $ExpectType boolean + $.Callbacks().locked(); + } + + function remove() { + // $ExpectType Callbacks + $.Callbacks().remove(() => { }, () => { }); + } +} + function EffectsOptions() { $('p').show({ always(animation, jumpToEnd) { From 6520173bf00e1f156c027a8cdbbcddba7405c019 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 12:22:40 -0400 Subject: [PATCH 06/35] [jquery] Fix Deferred method signatures. --- types/jquery/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 3d65135f5f..93bb3cb027 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4291,7 +4291,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.notifyWith/} * @since 1.7 */ - notifyWith(context: object, ...args: TNotify[]): this; + notifyWith(context: object, args?: ArrayLike): this; /** * Utility method to filter and/or chain Deferreds. * @@ -4324,7 +4324,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.rejectWith/} * @since 1.5 */ - rejectWith(context: object, ...args: TReject[]): this; + rejectWith(context: object, args?: ArrayLike): this; /** * Resolve a Deferred object and call any doneCallbacks with the given args. * @@ -4341,7 +4341,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.resolveWith/} * @since 1.5 */ - resolveWith(context: object, ...args: TResolve[]): this; + resolveWith(context: object, args?: ArrayLike): this; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * From 869c4ba5605d0dcdbe7804edae160dd2f984f1e3 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sat, 24 Jun 2017 12:57:14 -0400 Subject: [PATCH 07/35] [jquery] Refactor Event. Static members moved to a separate type. Removed originalTarget property. --- types/jquery/index.d.ts | 101 ++++++++++++++++++++--------------- types/jquery/jquery-tests.ts | 22 ++++++-- 2 files changed, 75 insertions(+), 48 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 93bb3cb027..7a1443e9ad 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2394,7 +2394,7 @@ interface JQuery { interface JQuery extends ArrayLike, Iterable { } interface JQueryStatic { - Event: JQuery.Event; + Event: JQuery.EventStatic; /** * Hook directly into jQuery to override how particular CSS properties are retrieved or set, normalize * CSS property naming, or create custom properties. @@ -4597,29 +4597,12 @@ declare namespace JQuery { // region Events + // region Event + // This should be a class but doesn't work correctly under the JQuery namespace. Event should be an inner class of jQuery. - interface Event { - /** - * The current DOM element within the event bubbling phase. - * - * @see {@link https://api.jquery.com/event.currentTarget/} - * @since 1.3 - */ - currentTarget: TTarget; - /** - * An optional object of data passed to an event method when the current executing handler is bound. - * - * @see {@link https://api.jquery.com/event.data/} - * @since 1.1 - */ - data: TData; - /** - * The element where the currently-called jQuery event handler was attached. - * - * @see {@link https://api.jquery.com/event.delegateTarget/} - * @since 1.7 - */ - delegateTarget: TTarget; + + // Instance members + interface Event { /** * Indicates whether the META key was pressed when the event fired. * @@ -4648,13 +4631,6 @@ declare namespace JQuery { * @since 1.0.4 */ pageY: number; - /** - * The other DOM element involved in the event, if any. - * - * @see {@link https://api.jquery.com/event.relatedTarget/} - * @since 1.1.4 - */ - relatedTarget: TTarget | null; /** * The last value returned by an event handler that was triggered by this event, unless the value was undefined. * @@ -4662,13 +4638,6 @@ declare namespace JQuery { * @since 1.3 */ result: any; - /** - * The DOM element that initiated the event. - * - * @see {@link https://api.jquery.com/event.target/} - * @since 1.0 - */ - target: TTarget; /** * The difference in milliseconds between the time the browser created the event and January 1, 1970. * @@ -4740,18 +4709,64 @@ declare namespace JQuery { stopPropagation(): void; } - interface Event extends Partial extends Partial> { - originalTarget?: TTarget; + /** + * The current DOM element within the event bubbling phase. + * + * @see {@link https://api.jquery.com/event.currentTarget/} + * @since 1.3 + */ + currentTarget: TTarget; + /** + * An optional object of data passed to an event method when the current executing handler is bound. + * + * @see {@link https://api.jquery.com/event.data/} + * @since 1.1 + */ + data: TData; + /** + * The element where the currently-called jQuery event handler was attached. + * + * @see {@link https://api.jquery.com/event.delegateTarget/} + * @since 1.7 + */ + delegateTarget: TTarget; originalEvent: _Event; - new(event: string, properties?: T): JQuery.Event & T; - new(properties: T): JQuery.Event & T; - (event: string, properties?: T): JQuery.Event & T; - (properties: T): JQuery.Event & T; + /** + * The other DOM element involved in the event, if any. + * + * @see {@link https://api.jquery.com/event.relatedTarget/} + * @since 1.1.4 + */ + relatedTarget: TTarget | null; + /** + * The DOM element that initiated the event. + * + * @see {@link https://api.jquery.com/event.target/} + * @since 1.0 + */ + target: TTarget; } + // Static members + interface EventStatic { + (event: string, properties?: T): JQuery.Event & T; + (properties: T): JQuery.Event & T; + new (event: string, properties?: T): JQuery.Event & T; + new (properties: T): JQuery.Event & T; + } + + interface EventLike { + type: string; + } + + // endregion + // Extra parameters can be passed from trigger() interface EventHandler { (this: TContext, eventObject: JQuery.Event, ...args: any[]): void | false | any; diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 1fda6e2da8..e88a470610 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4765,12 +4765,24 @@ function EffectsOptions() { } function _Event() { - function mixin() { - const e = $.Event('keydown', { - mySpecialKeyCode: JQuery.Key.CapsLock, - }); + function call_signature() { + // $ExpectType Event & Coordinates + $.Event('keydown', $('p').offset()); - e.mySpecialKeyCode === JQuery.Key.NumLock; + // $ExpectType Event & { type: string; } + $.Event({ + type: 'keydown' + }); + } + + function constructor() { + // $ExpectType Event & Coordinates + new $.Event('keydown', $('p').offset()); + + // $ExpectType Event & { type: string; } + new $.Event({ + type: 'keydown' + }); } } From 22d76950f9ece9f35e9de4ab64b431b6ccd6b859 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 25 Jun 2017 13:52:07 -0400 Subject: [PATCH 08/35] [jquery] Add Promise type that supports 3 parameters. --- types/jquery/index.d.ts | 553 +++++++++++++++++++++-------- types/jquery/jquery-tests.ts | 306 ++++++++++++++-- types/jquery/test/example-tests.ts | 9 +- types/jquery/tslint.json | 5 +- 4 files changed, 682 insertions(+), 191 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 7a1443e9ad..309c13d5f3 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3265,9 +3265,20 @@ interface JQueryStatic { * @since 1.12-2.2 */ uniqueSort(array: T[]): T[]; - when(jqxhr1: JQuery.jqXHR, jqxhr2: JQuery.jqXHR, jqxhr3: JQuery.jqXHR): JQuery.Promise<[T | U | V, string, JQuery.jqXHR]>; - when(jqxhr1: JQuery.jqXHR, jqxhr2: JQuery.jqXHR): JQuery.Promise<[T | U, string, JQuery.jqXHR]>; - when(jqxhr1: JQuery.jqXHR): JQuery.Promise>; + when(deferred1: T | JQuery.Thenable, + deferred2: U | JQuery.Thenable, + deferred3: V | JQuery.Thenable): JQuery.Promise3; + when(deferred: T | JQuery.Thenable): JQuery.Promise; + when + (promiseA: JQuery.Promise3, + promiseB: JQuery.Promise3, + promiseC: JQuery.Promise3): JQuery.Promise3<[AR1, AR2, AR3], [AJ1, AJ2, AJ3], [AN1, AN2, AN3], + [BR1, BR2, BR3], [BJ1, BJ2, BJ3], [BN1, BN2, BN3], + [CR1, CR2, CR3], [CJ1, CJ2, CJ3], [CN1, CN2, CN3]>; + when + (promiseA: JQuery.Promise3): JQuery.Promise3; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -4030,97 +4041,14 @@ declare namespace JQuery { /** * @see {@link http://api.jquery.com/jquery.ajax/#jqXHR} */ - interface jqXHR extends Pick, + interface jqXHR extends Promise3, never, + Ajax.SuccessTextStatus, Ajax.ErrorTextStatus, never, + jqXHR, string, never>, + Pick, Partial> { responseJSON?: any; - /** - * Add handlers to be called when the Deferred object is either resolved or rejected. - * - * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. - * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. - * @see {@link https://api.jquery.com/deferred.always/} - * @since 1.6 - */ - always(alwaysCallback: TypeOrArray>, - ...alwaysCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failFilter A function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.catch/} - * @since 3.0 - */ - catch(failFilter: (jqXHR: this, textStatus: Ajax.ErrorTextStatus, errorThrown: string) => never): Deferred; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failFilter A function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.catch/} - * @since 3.0 - */ - catch(failFilter: (jqXHR: this, textStatus: Ajax.ErrorTextStatus, errorThrown: string) => UResolve | Thenable): Deferred; - /** - * Add handlers to be called when the Deferred object is resolved. - * - * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. - * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.done/} - * @since 1.5 - */ - done(doneCallback: TypeOrArray>, - ...doneCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. - * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.fail/} - * @since 1.5 - */ - fail(failCallback: TypeOrArray>, - ...failCallbacks: Array>>): this; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe(doneFilter: ((data: TResolve, textStatus: Ajax.SuccessTextStatus, jqXHR: this) => UResolve | Thenable) | null, - failFilter?: ((jqXHR: this, textStatus: Ajax.ErrorTextStatus, errorThrown: string) => UReject | Thenable) | null): Deferred; - // jQuery doesn't send progress notifications so progress callbacks will never be called. - // Leaving progress() in however allows for structural compatibility with JQuery.Promise. - /** - * Add handlers to be called when the Deferred object generates progress notifications. - * - * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. - * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates - * progress notifications. - * @see {@link https://api.jquery.com/deferred.progress/} - * @since 1.7 - */ - progress(progressCallback: TypeOrArray, - ...progressCallbacks: Array>): this; - /** - * Return a Deferred's Promise object. - * - * @param target Object onto which the promise methods have to be attached - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(target: TTarget): JQuery.Promise & TTarget; - /** - * Return a Deferred's Promise object. - * - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(): JQuery.Promise; /** * Determine the current state of a Deferred object. * @@ -4129,31 +4057,23 @@ declare namespace JQuery { */ state(): 'pending' | 'resolved' | 'rejected'; statusCode(map: Ajax.StatusCodeCallbacks): void; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then(doneFilter: ((data: TResolve, textStatus: Ajax.SuccessTextStatus, jqXHR: this) => UResolve | Thenable) | null, - failFilter?: ((jqXHR: this, textStatus: Ajax.ErrorTextStatus, errorThrown: string) => UReject | Thenable) | null): Deferred; } namespace jqXHR { - interface DoneCallback { - (data: TResolve, textStatus: Ajax.SuccessTextStatus, jqXHR: jqXHR): void; - } + /** + * @deprecated + */ + interface DoneCallback> extends Deferred.Callback3 { } - interface FailCallback { - (jqXHR: TResolve, textStatus: Ajax.ErrorTextStatus, errorThrown: string): void; - } + /** + * @deprecated + */ + interface FailCallback extends Deferred.Callback3 { } - interface AlwaysCallback { - (data_jqXHR: TResolve | jqXHR, textStatus: Ajax.TextStatus, jqXHR_errorThrown: jqXHR | string): void; - } + /** + * @deprecated + */ + interface AlwaysCallback> extends Deferred.Callback3 { } } // endregion @@ -4275,6 +4195,37 @@ declare namespace JQuery { * @since 3.0 */ catch(failFilter: (...reasons: TReject[]) => UResolve | Thenable): Deferred; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, + failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, + progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, + failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, + progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; + /** * Call the progressCallbacks on a Deferred object with the given args. * @@ -4292,22 +4243,6 @@ declare namespace JQuery { * @since 1.7 */ notifyWith(context: object, args?: ArrayLike): this; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, - failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; /** * Reject a Deferred object and call any failCallbacks with the given args. * @@ -4342,38 +4277,40 @@ declare namespace JQuery { * @since 1.5 */ resolveWith(context: object, args?: ArrayLike): this; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, - failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; } namespace Deferred { - interface DoneCallback { - (...values: TResolve[]): void; + interface Callback3 { + (t: T, u: U, v: V): void; } - interface FailCallback { - (...reasons: TReject[]): void; + interface Callback2 { + (t: T, u: U): void; } - interface AlwaysCallback { - (...values_reasons: Array): void; + interface Callback { + (...args: T[]): void; } - interface ProgressCallback { - (...values: TNotify[]): void; - } + /** + * @deprecated + */ + interface DoneCallback extends Callback { } + + /** + * @deprecated + */ + interface FailCallback extends Callback { } + + /** + * @deprecated + */ + interface AlwaysCallback extends Callback { } + + /** + * @deprecated + */ + interface ProgressCallback extends Callback { } // Common interface for Deferred and Promise interface PromiseBase { @@ -4443,6 +4380,318 @@ declare namespace JQuery { } } + /** + * This object provides a subset of the methods of the Deferred object (then, done, fail, always, + * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. + * + * @see {@link http://api.jquery.com/Types/#Promise} + */ + interface Promise3 { + /** + * Add handlers to be called when the Deferred object is either resolved or rejected. + * + * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. + * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. + * @see {@link https://api.jquery.com/deferred.always/} + * @since 1.6 + */ + always(alwaysCallback: TypeOrArray>, + ...alwaysCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is resolved. + * + * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. + * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.done/} + * @since 1.5 + */ + done(doneCallback: TypeOrArray>, + ...doneCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. + * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.fail/} + * @since 1.5 + */ + fail(failCallback: TypeOrArray>, + ...failCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object generates progress notifications. + * + * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. + * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates + * progress notifications. + * @see {@link https://api.jquery.com/deferred.progress/} + * @since 1.7 + */ + progress(progressCallback: TypeOrArray>, + ...progressCallbacks: Array>>): this; + /** + * Return a Deferred's Promise object. + * + * @param target Object onto which the promise methods have to be attached + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(target: TTarget): this & TTarget; + /** + * Return a Deferred's Promise object. + * + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(): this; + /** + * Determine the current state of a Deferred object. + * + * @see {@link https://api.jquery.com/deferred.state/} + * @since 1.7 + */ + state(): 'pending' | 'resolved' | 'rejected'; + + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failFilter A function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.catch/} + * @since 3.0 + */ + catch + (failFilter: (t: TJ, u: UJ, v: VJ) => AR | Thenable | Promise3): Promise3; + + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3, + progressFilter: (t: TN, u: UN, v: VN) => AN3 | Thenable | Promise3): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => AR1 | Thenable | Promise3, + progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => AN | Thenable | Promise3): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => AR | Thenable | Promise3): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => AR | Thenable | Promise3): Promise3; + + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3, + progressFilter: (t: TN, u: UN, v: VN) => AN3 | Thenable | Promise3): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => AR1 | Thenable | Promise3, + progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => AN | Thenable | Promise3): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => AR | Thenable | Promise3): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => AR | Thenable | Promise3): Promise3; + } + /** * This object provides a subset of the methods of the Deferred object (then, done, fail, always, * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. @@ -4473,7 +4722,7 @@ declare namespace JQuery { UReject = TReject, UNotify = TNotify>(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Promise; + progressFilter?: ((...values: TNotify[]) => UNotify | Thenable) | null): Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4487,7 +4736,7 @@ declare namespace JQuery { UReject = TReject, UNotify = TNotify>(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Promise; + progressFilter?: ((...values: TNotify[]) => UNotify | Thenable) | null): Promise; } // endregion diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index e88a470610..f955d3708c 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4433,24 +4433,172 @@ function JQueryStatic() { } function when() { - const t = $.ajax() as JQuery.jqXHR; - const u = $.ajax() as JQuery.jqXHR; - const v = $.ajax() as JQuery.jqXHR; + function Promise3() { + const t = $.ajax() as JQuery.jqXHR; + const u = $.ajax() as JQuery.jqXHR; + const v = $.ajax() as JQuery.jqXHR; - // $ExpectType Promise<[string | number | boolean, string, jqXHR], any, any> - $.when(t, u, v); + // 3 parameters + { + const w = $.when(t, u, v); - // $ExpectType Promise<[string | number, string, jqXHR], any, any> - $.when(t, u); + w.then((a, b, c) => { + // $ExpectType [string, SuccessTextStatus, jqXHR] + a; + // $ExpectType [number, SuccessTextStatus, jqXHR] + b; + // $ExpectType [boolean, SuccessTextStatus, jqXHR] + c; + }); + w.catch((a, b, c) => { + // $ExpectType [jqXHR, ErrorTextStatus, string] + a; + // $ExpectType [jqXHR, ErrorTextStatus, string] + b; + // $ExpectType [jqXHR, ErrorTextStatus, string] + c; + }); + w.then(null, null, (a, b, c) => { + // $ExpectType [never, never, never] + a; + // $ExpectType [never, never, never] + b; + // $ExpectType [never, never, never] + c; + }); + } - // $ExpectType Promise, any, any> - $.when(t); + // 2 parameters + { + const w = $.when(t, u); + + w.then((a, b) => { + // $ExpectType [string, SuccessTextStatus, jqXHR] + a; + // $ExpectType [number, SuccessTextStatus, jqXHR] + b; + }); + w.catch((a, b) => { + // $ExpectType [jqXHR, ErrorTextStatus, string] + a; + // $ExpectType [jqXHR, ErrorTextStatus, string] + b; + }); + w.then(null, null, (a, b) => { + // $ExpectType [never, never, never] + a; + // $ExpectType [never, never, never] + b; + }); + } + + // 1 parameter + { + const w = $.when(t); + + w.then((data, textStatus, jqXHR) => { + // $ExpectType string + data; + // $ExpectType SuccessTextStatus + textStatus; + // $ExpectType jqXHR + jqXHR; + }); + w.catch((jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }); + w.then(null, null, (a, b, c) => { + // $ExpectType never + a; + // $ExpectType never + b; + // $ExpectType never + c; + }); + } + } // $ExpectType Promise $.when($.Deferred()); // $ExpectType Promise $.when(); + + // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/2725 + function issue_2725() { + function first() { + return $.when('1'); + } + + $.when().then(() => { + const f = first(); + // $ExpectType Promise + f; + + return f; + }).then((value) => { + // $ExpectType string + value; + }); + } + + // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/10159 + function issue_10159() { + interface One { result: number; } + interface Two { Result: number; } + interface Three { TheResult: number; } + + class AsyncRunner { + Run(): void { + const task1 = this.runTask1(); + const task2 = this.runTask2(); + const task3 = this.runTask3(); + + // $ExpectType Promise + task1; + // $ExpectType Promise + task2; + // $ExpectType Promise + task3; + + $.when(task1, task2, task3) + .done((r1, r2, r3) => { + // $ExpectType One + r1; + // $ExpectType Two + r2; + // $ExpectType Three + r3; + }); + } + + runTask1() { + let dfd = $.Deferred(); + console.log('Task 1'); + setTimeout(() => { dfd.resolve({ result: 1 }); }, Math.floor(400 + Math.random() * 2000)); + return dfd.promise(); + } + + runTask2() { + let dfd = $.Deferred(); + console.log('Task 2'); + setTimeout(() => { dfd.resolve({ Result: 2 }); }, Math.floor(400 + Math.random() * 2000)); + return dfd.promise(); + } + + runTask3() { + let dfd = $.Deferred(); + console.log('Task 3'); + setTimeout(() => { dfd.resolve({ TheResult: 3 }); }, Math.floor(400 + Math.random() * 2000)); + return dfd.promise(); + } + } + } } } @@ -5028,7 +5176,7 @@ function jqXHR() { } function _catch() { - // $ExpectType Deferred + // $ExpectType Promise3 $.ajax('/echo').catch((jqXHR, textStatus, errorThrown) => { // $ExpectType jqXHR jqXHR; @@ -5039,30 +5187,126 @@ function jqXHR() { }); } - function catch_throw_returnType() { - // $ExpectType Deferred - $.ajax('/echo').catch((reason) => { - throw new Error(); - }); - } - function then_returnType() { - // $ExpectType Deferred - $.ajax('/echo').then(() => { }); + // $ExpectType Promise3, never, never, ErrorTextStatus, never, never, string, never> + $.ajax('/echo/json').then(() => { }); + } +} + +function Promise3() { + function then() { + function doneFilter() { + // $ExpectType Promise3, never, never, ErrorTextStatus, never, never, string, never> + $.ajax('/echo/json').then(() => { + return 1; + }); + + // $ExpectType Promise3, never, never, ErrorTextStatus, never, never, string, never> + $.ajax('/echo/json').then(() => { + const t: JQuery.Thenable = { + then() { + return Promise.resolve('myValue'); + } + }; + + return t; + }); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(() => { + return $.ajax('/echo/json'); + }); + } + + function null_failFilter() { + // $ExpectType Promise3 + $.ajax('/echo/json').then(null, () => { + return 1; + }); + + // $ExpectType Promise3 + $.ajax('/echo/json').then(null, () => { + const t: JQuery.Thenable = { + then() { + return Promise.resolve('myValue'); + } + }; + + return t; + }); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(null, () => { + return $.ajax('/echo/json'); + }); + } + + function doneFilter_failFilter() { + const value = () => { + return 1; + }; + const thenable = () => { + const t: JQuery.Thenable = { + then() { + return Promise.resolve('myValue'); + } + }; + + return t; + }; + const promise3 = () => { + return $.ajax('/echo/json') as JQuery.jqXHR; + }; + + // $ExpectType Promise3 + $.ajax('/echo/json').then(value, value); + + // $ExpectType Promise3 + $.ajax('/echo/json').then(thenable, thenable); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(promise3, promise3); + + // $ExpectType Promise3 + $.ajax('/echo/json').then(value, thenable); + + // $ExpectType Promise3 + $.ajax('/echo/json').then(thenable, value); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(value, promise3); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(promise3, value); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(thenable, promise3); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').then(promise3, thenable); + } } - function throw_from_catch() { - $.ajax('/echo').catch(() => { - throw new Error('Thrown from [jQuery] 1st catch block'); - }).then((value) => { - // $ExpectType never - value; - }).catch((reason) => { - // $ExpectType any - reason; - }).then((value) => { - // $ExpectType void - value; + function _catch() { + // $ExpectType Promise3 + $.ajax('/echo/json').catch(() => { + return 1; + }); + + // $ExpectType Promise3 + $.ajax('/echo/json').catch(() => { + const t: JQuery.Thenable = { + then() { + return Promise.resolve('myValue'); + } + }; + + return t; + }); + + // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> + $.ajax('/echo/json').catch(() => { + return $.ajax('/echo/json'); }); } } diff --git a/types/jquery/test/example-tests.ts b/types/jquery/test/example-tests.ts index 404aba2013..f59f8a3acc 100644 --- a/types/jquery/test/example-tests.ts +++ b/types/jquery/test/example-tests.ts @@ -1202,14 +1202,13 @@ function examples() { // Attach a done, fail, and progress handler for the asyncEvent $.when(asyncEvent()).then( function(status) { - status === 3; alert(status + ', things are going well'); }, function(status) { alert(status + ', you fail this time'); }, function(status) { - // $('body').append(status); + $('body').append(status); }, ); } @@ -1233,9 +1232,7 @@ function examples() { // Use the object as a Promise _obj.done(function(name) { _obj.hello(name); // Will alert "Hello John" - }); - /// TODO: This doesn't work even though .done() returns this - // .hello('Karl'); // Will alert "Hello Karl" + }).hello('Karl'); // Will alert "Hello Karl" } function deferred_then_0() { @@ -1392,7 +1389,7 @@ function examples() { function each_2() { $('button').click(function() { $('div').each(function(index, element) { - // element == this + // element == this; $(element).css('backgroundColor', 'yellow'); if ($(this).is('#stop')) { $('span').text('Stopped at div index #' + index); diff --git a/types/jquery/tslint.json b/types/jquery/tslint.json index 401b7a77e9..ca514c4aba 100644 --- a/types/jquery/tslint.json +++ b/types/jquery/tslint.json @@ -2,8 +2,9 @@ "extends": "dtslint/dt.json", "rules": { "ban-types": false, - "no-misused-new": false, + "callable-types": false, "no-empty-interface": false, - "callable-types": false + "no-misused-new": false, + "space-before-function-paren": false } } From 85c09b5387f3453894c5d80b30af7f7fe7c5f4ae Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 25 Jun 2017 15:02:46 -0400 Subject: [PATCH 09/35] [jquery] Add Promise type that supports 2 parameters. --- types/jquery/index.d.ts | 399 +++++++++++++++++++++++++++++++++-- types/jquery/jquery-tests.ts | 90 +++++++- 2 files changed, 471 insertions(+), 18 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 309c13d5f3..2fc6a38f22 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3265,20 +3265,101 @@ interface JQueryStatic { * @since 1.12-2.2 */ uniqueSort(array: T[]): T[]; - when(deferred1: T | JQuery.Thenable, - deferred2: U | JQuery.Thenable, - deferred3: V | JQuery.Thenable): JQuery.Promise3; - when(deferred: T | JQuery.Thenable): JQuery.Promise; - when - (promiseA: JQuery.Promise3, - promiseB: JQuery.Promise3, - promiseC: JQuery.Promise3): JQuery.Promise3<[AR1, AR2, AR3], [AJ1, AJ2, AJ3], [AN1, AN2, AN3], - [BR1, BR2, BR3], [BJ1, BJ2, BJ3], [BN1, BN2, BN3], - [CR1, CR2, CR3], [CJ1, CJ2, CJ3], [CN1, CN2, CN3]>; - when - (promiseA: JQuery.Promise3): JQuery.Promise3; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when + (deferredT: TR1 | JQuery.Thenable | JQuery.Promise, + deferredU: UR1 | JQuery.Thenable | JQuery.Promise, + deferredV: VR1 | JQuery.Thenable | JQuery.Promise): JQuery.Promise3; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when + (deferredT: JQuery.Promise2 | + JQuery.Promise3, + deferredU: JQuery.Promise2 | + JQuery.Promise3, + deferredV: JQuery.Promise2 | + JQuery.Promise3): JQuery.Promise3<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], [TN1, TN2, TN3], + [UR1, UR2, UR3], [UJ1, UJ2, UJ3], [UN1, UN2, UN3], + [VR1, VR2, VR3], [VJ1, VJ2, VJ3], [VN1, VN2, VN3]>; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when + (deferredT: TR1 | JQuery.Thenable | JQuery.Promise, + deferredU: UR1 | JQuery.Thenable | JQuery.Promise): JQuery.Promise2; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when + (deferredT: JQuery.Promise2 | + JQuery.Promise3, + deferredU: JQuery.Promise2 | + JQuery.Promise3): JQuery.Promise2<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], [TN1, TN2, TN3], + [UR1, UR2, UR3], [UJ1, UJ2, UJ3], [UN1, UN2, UN3]>; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when(deferred: TR1 | JQuery.Thenable | JQuery.Promise): JQuery.Promise; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when + (deferredT: JQuery.Promise3): JQuery.Promise3; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when + (deferredT: JQuery.Promise2): JQuery.Promise2; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3287,7 +3368,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(...deferreds: any[]): JQuery.Promise; + when(...deferreds: Array>): JQuery.Promise; } declare namespace JQuery { @@ -4692,6 +4773,294 @@ declare namespace JQuery { (doneFilter: (t: TR, u: UR, v: VR) => AR | Thenable | Promise3): Promise3; } + /** + * This object provides a subset of the methods of the Deferred object (then, done, fail, always, + * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. + * + * @see {@link http://api.jquery.com/Types/#Promise} + */ + interface Promise2 { + /** + * Add handlers to be called when the Deferred object is either resolved or rejected. + * + * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. + * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. + * @see {@link https://api.jquery.com/deferred.always/} + * @since 1.6 + */ + always(alwaysCallback: TypeOrArray>, + ...alwaysCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is resolved. + * + * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. + * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.done/} + * @since 1.5 + */ + done(doneCallback: TypeOrArray>, + ...doneCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. + * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.fail/} + * @since 1.5 + */ + fail(failCallback: TypeOrArray>, + ...failCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object generates progress notifications. + * + * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. + * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates + * progress notifications. + * @see {@link https://api.jquery.com/deferred.progress/} + * @since 1.7 + */ + progress(progressCallback: TypeOrArray>, + ...progressCallbacks: Array>>): this; + /** + * Return a Deferred's Promise object. + * + * @param target Object onto which the promise methods have to be attached + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(target: TTarget): this & TTarget; + /** + * Return a Deferred's Promise object. + * + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(): this; + /** + * Determine the current state of a Deferred object. + * + * @see {@link https://api.jquery.com/deferred.state/} + * @since 1.7 + */ + state(): 'pending' | 'resolved' | 'rejected'; + + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failFilter A function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.catch/} + * @since 3.0 + */ + catch + (failFilter: (t: TJ, u: UJ) => AR | Thenable | Promise2): Promise2; + + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2, + progressFilter: (t: TN, u: UN) => AN3 | Thenable | Promise2): Promise2; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (t: TJ, u: UJ) => AR1 | Thenable | Promise2, + progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + failFilter: null, + progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: null, + progressFilter: (t: TN, u: UN) => AN | Thenable | Promise2): Promise2; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2): Promise2; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (t: TJ, u: UJ) => AR | Thenable | Promise2): Promise2; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR) => AR | Thenable | Promise2): Promise2; + + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2, + progressFilter: (t: TN, u: UN) => AN3 | Thenable | Promise2): Promise2; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (t: TJ, u: UJ) => AR1 | Thenable | Promise2, + progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + failFilter: null, + progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: null, + progressFilter: (t: TN, u: UN) => AN | Thenable | Promise2): Promise2; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2): Promise2; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (t: TJ, u: UJ) => AR | Thenable | Promise2): Promise2; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR) => AR | Thenable | Promise2): Promise2; + } + /** * This object provides a subset of the methods of the Deferred object (then, done, fail, always, * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index f955d3708c..478e1390a4 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4523,10 +4523,94 @@ function JQueryStatic() { } } - // $ExpectType Promise - $.when($.Deferred()); + function Promise2() { + const t = $.Deferred() as JQuery.Promise2; + const u = $.Deferred() as JQuery.Promise2; + const v = $.Deferred() as JQuery.Promise2; - // $ExpectType Promise + // 3 parameters + { + const w = $.when(t, u, v); + + w.then((a, b, c) => { + // $ExpectType [string, boolean, never] + a; + // $ExpectType [string, boolean, never] + b; + // $ExpectType [string, boolean, never] + c; + }); + w.catch((a, b, c) => { + // $ExpectType [Error, any, never] + a; + // $ExpectType [Error, any, never] + b; + // $ExpectType [Error, any, never] + c; + }); + w.then(null, null, (a, b, c) => { + // $ExpectType [number, any, never] + a; + // $ExpectType [number, any, never] + b; + // $ExpectType [number, any, never] + c; + }); + } + + // 2 parameters + { + const w = $.when(t, u); + + w.then((a, b) => { + // $ExpectType [string, boolean, never] + a; + // $ExpectType [string, boolean, never] + b; + }); + w.catch((a, b) => { + // $ExpectType [Error, any, never] + a; + // $ExpectType [Error, any, never] + b; + }); + w.then(null, null, (a, b) => { + // $ExpectType [number, any, never] + a; + // $ExpectType [number, any, never] + b; + }); + } + + // 1 parameter + { + const w = $.when(t); + + w.then((a, b) => { + // $ExpectType string + a; + // $ExpectType boolean + b; + }); + w.catch((a, b) => { + // $ExpectType Error + a; + // $ExpectType any + b; + }); + w.then(null, null, (a, b) => { + // $ExpectType number + a; + // $ExpectType any + b; + }); + } + } + + // $ExpectType Promise + $.when($.Deferred()); + + // $ExpectType Promise $.when(); // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/2725 From 86def88082336825a641ad492add423234779af6 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 25 Jun 2017 15:54:08 -0400 Subject: [PATCH 10/35] [jquery] Fix Deferred and Promise. --- types/jquery/index.d.ts | 559 +++++++++++++++++++++++++++++++++------- 1 file changed, 462 insertions(+), 97 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 2fc6a38f22..316e631b9d 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4267,7 +4267,71 @@ declare namespace JQuery { */ interface Thenable extends PromiseLike { } - interface Deferred extends Deferred.PromiseBase { + interface Deferred { + /** + * Add handlers to be called when the Deferred object is either resolved or rejected. + * + * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. + * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. + * @see {@link https://api.jquery.com/deferred.always/} + * @since 1.6 + */ + always(alwaysCallback: TypeOrArray>, + ...alwaysCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is resolved. + * + * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. + * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.done/} + * @since 1.5 + */ + done(doneCallback: TypeOrArray>, + ...doneCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. + * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.fail/} + * @since 1.5 + */ + fail(failCallback: TypeOrArray>, + ...failCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object generates progress notifications. + * + * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. + * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates + * progress notifications. + * @see {@link https://api.jquery.com/deferred.progress/} + * @since 1.7 + */ + progress(progressCallback: TypeOrArray>, + ...progressCallbacks: Array>>): this; + /** + * Return a Deferred's Promise object. + * + * @param target Object onto which the promise methods have to be attached + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(target: TTarget): JQuery.Promise & TTarget; + /** + * Return a Deferred's Promise object. + * + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(): JQuery.Promise; + /** + * Determine the current state of a Deferred object. + * + * @see {@link https://api.jquery.com/deferred.state/} + * @since 1.7 + */ + state(): 'pending' | 'resolved' | 'rejected'; + /** * Add handlers to be called when the Deferred object is rejected. * @@ -4275,7 +4339,8 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.catch/} * @since 3.0 */ - catch(failFilter: (...reasons: TReject[]) => UResolve | Thenable): Deferred; + catch(failFilter: (...reasons: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** * Utility method to filter and/or chain Deferreds. * @@ -4287,11 +4352,93 @@ declare namespace JQuery { * @since 1.7 * @deprecated 1.8 */ - pipe(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, - failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; + pipe + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: null, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: null, + progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4301,11 +4448,80 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.then/} * @since 1.8 */ - then(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, - failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => TNotify | Thenable) | null): Deferred; + then + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: null, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: null, + progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; /** * Call the progressCallbacks on a Deferred object with the given args. @@ -4314,7 +4530,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.notify/} * @since 1.7 */ - notify(...args: TNotify[]): this; + notify(...args: TN[]): this; /** * Call the progressCallbacks on a Deferred object with the given context and args. * @@ -4323,7 +4539,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.notifyWith/} * @since 1.7 */ - notifyWith(context: object, args?: ArrayLike): this; + notifyWith(context: object, args?: ArrayLike): this; /** * Reject a Deferred object and call any failCallbacks with the given args. * @@ -4331,7 +4547,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.reject/} * @since 1.5 */ - reject(...args: TReject[]): this; + reject(...args: TJ[]): this; /** * Reject a Deferred object and call any failCallbacks with the given context and args. * @@ -4340,7 +4556,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.rejectWith/} * @since 1.5 */ - rejectWith(context: object, args?: ArrayLike): this; + rejectWith(context: object, args?: ArrayLike): this; /** * Resolve a Deferred object and call any doneCallbacks with the given args. * @@ -4348,7 +4564,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.resolve/} * @since 1.5 */ - resolve(...args: TResolve[]): this; + resolve(...args: TR[]): this; /** * Resolve a Deferred object and call any doneCallbacks with the given context and args. * @@ -4357,7 +4573,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.resolveWith/} * @since 1.5 */ - resolveWith(context: object, args?: ArrayLike): this; + resolveWith(context: object, args?: ArrayLike): this; } namespace Deferred { @@ -4392,73 +4608,6 @@ declare namespace JQuery { * @deprecated */ interface ProgressCallback extends Callback { } - - // Common interface for Deferred and Promise - interface PromiseBase { - /** - * Add handlers to be called when the Deferred object is either resolved or rejected. - * - * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. - * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. - * @see {@link https://api.jquery.com/deferred.always/} - * @since 1.6 - */ - always(alwaysCallback: TypeOrArray>, - ...alwaysCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is resolved. - * - * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. - * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.done/} - * @since 1.5 - */ - done(doneCallback: TypeOrArray>, - ...doneCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. - * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.fail/} - * @since 1.5 - */ - fail(failCallback: TypeOrArray>, - ...failCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object generates progress notifications. - * - * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. - * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates - * progress notifications. - * @see {@link https://api.jquery.com/deferred.progress/} - * @since 1.7 - */ - progress(progressCallback: TypeOrArray>, - ...progressCallbacks: Array>>): this; - /** - * Return a Deferred's Promise object. - * - * @param target Object onto which the promise methods have to be attached - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(target: TTarget): JQuery.Promise & TTarget; - /** - * Return a Deferred's Promise object. - * - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(): JQuery.Promise; - /** - * Determine the current state of a Deferred object. - * - * @see {@link https://api.jquery.com/deferred.state/} - * @since 1.7 - */ - state(): 'pending' | 'resolved' | 'rejected'; - } } /** @@ -5067,7 +5216,71 @@ declare namespace JQuery { * * @see {@link http://api.jquery.com/Types/#Promise} */ - interface Promise extends Deferred.PromiseBase { + interface Promise { + /** + * Add handlers to be called when the Deferred object is either resolved or rejected. + * + * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. + * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. + * @see {@link https://api.jquery.com/deferred.always/} + * @since 1.6 + */ + always(alwaysCallback: TypeOrArray>, + ...alwaysCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is resolved. + * + * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. + * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.done/} + * @since 1.5 + */ + done(doneCallback: TypeOrArray>, + ...doneCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object is rejected. + * + * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. + * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.fail/} + * @since 1.5 + */ + fail(failCallback: TypeOrArray>, + ...failCallbacks: Array>>): this; + /** + * Add handlers to be called when the Deferred object generates progress notifications. + * + * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. + * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates + * progress notifications. + * @see {@link https://api.jquery.com/deferred.progress/} + * @since 1.7 + */ + progress(progressCallback: TypeOrArray>, + ...progressCallbacks: Array>>): this; + /** + * Return a Deferred's Promise object. + * + * @param target Object onto which the promise methods have to be attached + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(target: TTarget): JQuery.Promise & TTarget; + /** + * Return a Deferred's Promise object. + * + * @see {@link https://api.jquery.com/deferred.promise/} + * @since 1.5 + */ + promise(): JQuery.Promise; + /** + * Determine the current state of a Deferred object. + * + * @see {@link https://api.jquery.com/deferred.state/} + * @since 1.7 + */ + state(): 'pending' | 'resolved' | 'rejected'; + /** * Add handlers to be called when the Deferred object is rejected. * @@ -5075,7 +5288,8 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.catch/} * @since 3.0 */ - catch(failFilter: (...reasons: TReject[]) => UReject | Thenable): Promise; + catch(failFilter: (...reasons: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** * Utility method to filter and/or chain Deferreds. * @@ -5087,11 +5301,93 @@ declare namespace JQuery { * @since 1.7 * @deprecated 1.8 */ - pipe(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, - failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => UNotify | Thenable) | null): Promise; + pipe + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: null, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: null, + progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5101,11 +5397,80 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.then/} * @since 1.8 */ - then(doneFilter: ((...values: TResolve[]) => UResolve | Thenable) | null, - failFilter?: ((...reasons: TReject[]) => UReject | Thenable) | null, - progressFilter?: ((...values: TNotify[]) => UNotify | Thenable) | null): Promise; + then + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: null, + progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: null, + progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; } // endregion From 8532f27673f13da5a70ceb3ea53394550c4b0363 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 25 Jun 2017 17:07:59 -0400 Subject: [PATCH 11/35] [jquery] Add more tests for JQueryStatic.when(). --- types/jquery/jquery-tests.ts | 58 +++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 478e1390a4..376eaa51f6 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4607,11 +4607,61 @@ function JQueryStatic() { } } - // $ExpectType Promise - $.when($.Deferred()); + function Promise() { + const w = $.when($.Deferred()); - // $ExpectType Promise - $.when(); + w.then(value => { + // $ExpectType string + value; + }); + + w.catch(reason => { + // $ExpectType Error + reason; + }); + + w.then(null, null, value => { + // $ExpectType number + value; + }); + } + + function Thenable() { + const w = $.when($.Deferred()); + + w.then(value => { + // $ExpectType string + value; + }); + } + + function value() { + const w = $.when('myVal1'); + + w.then(value => { + // $ExpectType string + value; + }); + } + + function Zero() { + const w = $.when(); + + w.then((value) => { + // $ExpectType never + value; + }); + + w.catch((reason) => { + // $ExpectType never + reason; + }); + + w.then(null, null, (value) => { + // $ExpectType never + value; + }); + } // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/2725 function issue_2725() { From 2ff6e2cad4b110ddaa082b21bc556b1e04dfff5b Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 08:00:00 -0400 Subject: [PATCH 12/35] [jquery] offset() can return undefined on empty sets. --- types/jquery/index.d.ts | 2 +- types/jquery/jquery-tests.ts | 2 +- types/jquery/test/example-tests.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 316e631b9d..9e6a67e440 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -1368,7 +1368,7 @@ interface JQuery { * @see {@link https://api.jquery.com/offset/} * @since 1.2 */ - offset(): JQuery.Coordinates; + offset(): JQuery.Coordinates | undefined; /** * Get the closest ancestor element that is positioned. * diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 376eaa51f6..e2cea8ba54 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -601,7 +601,7 @@ function JQuery() { }; }); - // $ExpectType Coordinates + // $ExpectType Coordinates | undefined $('p').offset(); } diff --git a/types/jquery/test/example-tests.ts b/types/jquery/test/example-tests.ts index f59f8a3acc..0c871de0a7 100644 --- a/types/jquery/test/example-tests.ts +++ b/types/jquery/test/example-tests.ts @@ -3754,13 +3754,13 @@ function examples() { function offset_0() { var p = $('p:last'); - var offset = p.offset(); + var offset = p.offset()!; p.html('left: ' + offset.left + ', top: ' + offset.top); } function offset_1() { $('*', document.body).click(function(event) { - var offset = $(this).offset(); + var offset = $(this).offset()!; event.stopPropagation(); $('#result').text(this.tagName + ' coords ( ' + offset.left + ', ' + offset.top + ' )'); From ad4c226d12fdbd2dd221d3de2356b9364fdb98c7 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 08:05:35 -0400 Subject: [PATCH 13/35] [jquery] Add @deprecated to ready(). --- types/jquery/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 9e6a67e440..e99260c2de 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -1793,6 +1793,7 @@ interface JQuery { * @param handler A function to execute after the DOM is ready. * @see {@link https://api.jquery.com/ready/} * @since 1.0 + * @deprecated 3.0 */ ready(handler: ($: JQueryStatic) => void): this; /** From b986d024e184a0f17790b937401dd0033febe38b Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 08:16:18 -0400 Subject: [PATCH 14/35] [jquery] Add Deferred.exceptionHook. --- types/jquery/index.d.ts | 29 ++++++++------- types/jquery/jquery-tests.ts | 70 +++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 45 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index e99260c2de..64111d5b69 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2395,6 +2395,16 @@ interface JQuery { interface JQuery extends ArrayLike, Iterable { } interface JQueryStatic { + /** + * A factory function that returns a chainable utility object with methods to register multiple + * callbacks into callback queues, invoke callback queues, and relay the success or failure state of + * any synchronous or asynchronous function. + * + * @param beforeStart A function that is called just before the constructor returns. + * @see {@link https://api.jquery.com/jQuery.Deferred/} + * @since 1.5 + */ + Deferred: JQuery.DeferredStatic; Event: JQuery.EventStatic; /** * Hook directly into jQuery to override how particular CSS properties are retrieved or set, normalize @@ -2501,19 +2511,6 @@ interface JQueryStatic { * @since 1.7 */ Callbacks(flags?: string): JQuery.Callbacks; - /** - * A factory function that returns a chainable utility object with methods to register multiple - * callbacks into callback queues, invoke callback queues, and relay the success or failure state of - * any synchronous or asynchronous function. - * - * @param beforeStart A function that is called just before the constructor returns. - * @see {@link https://api.jquery.com/jQuery.Deferred/} - * @since 1.5 - */ - Deferred(beforeStart?: (this: JQuery.Deferred, - deferred: JQuery.Deferred) => void): JQuery.Deferred; /** * Perform an asynchronous HTTP (Ajax) request. * @@ -4268,6 +4265,12 @@ declare namespace JQuery { */ interface Thenable extends PromiseLike { } + interface DeferredStatic { + // https://jquery.com/upgrade-guide/3.0/#callback-exit + exceptionHook: any; + (beforeStart?: (this: JQuery.Deferred, deferred: JQuery.Deferred) => void): JQuery.Deferred; + } + interface Deferred { /** * Add handlers to be called when the Deferred object is either resolved or rejected. diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index e2cea8ba54..8a6710a2fd 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -3592,49 +3592,55 @@ function JQueryStatic() { } function Deferred() { - // #ExpectType Deferred - $.Deferred(function(deferred) { + function call_signature() { // #ExpectType Deferred - this; + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + // #ExpectType Deferred - deferred; - }); + $.Deferred(); - // #ExpectType Deferred - $.Deferred(); - - // #ExpectType Deferred - $.Deferred(function(deferred) { // #ExpectType Deferred - this; + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + // #ExpectType Deferred - deferred; - }); + $.Deferred(); - // #ExpectType Deferred - $.Deferred(); - - // #ExpectType Deferred - $.Deferred(function(deferred) { // #ExpectType Deferred - this; + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); + // #ExpectType Deferred - deferred; - }); + $.Deferred(); - // #ExpectType Deferred - $.Deferred(); - - // #ExpectType Deferred - $.Deferred(function(deferred) { // #ExpectType Deferred - this; - // #ExpectType Deferred - deferred; - }); + $.Deferred(function(deferred) { + // #ExpectType Deferred + this; + // #ExpectType Deferred + deferred; + }); - // #ExpectType Deferred - $.Deferred(); + // #ExpectType Deferred + $.Deferred(); + } + + function exceptionHook() { + $.Deferred.exceptionHook = undefined; + } } function ajax() { From 79746b418281b8fc67b5dfbdfb253a6f7ce2f9ef Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 09:19:18 -0400 Subject: [PATCH 15/35] [jquery] when() does not emit progress notifications. --- types/jquery/index.d.ts | 58 +++++------ types/jquery/jquery-tests.ts | 192 +++++++++++++++++++++++------------ 2 files changed, 152 insertions(+), 98 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 64111d5b69..bae24d9780 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3273,9 +3273,11 @@ interface JQueryStatic { when - (deferredT: TR1 | JQuery.Thenable | JQuery.Promise, - deferredU: UR1 | JQuery.Thenable | JQuery.Promise, - deferredV: VR1 | JQuery.Thenable | JQuery.Promise): JQuery.Promise3; + (deferredT: JQuery.Promise | JQuery.Thenable | TR1, + deferredU: JQuery.Promise | JQuery.Thenable | UR1, + deferredV: JQuery.Promise | JQuery.Thenable | VR1): JQuery.Promise3; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3292,14 +3294,14 @@ interface JQueryStatic { VR1 = never, VJ1 = never, VN1 = never, VR2 = never, VJ2 = never, VN2 = never, VR3 = never, VJ3 = never, VN3 = never> - (deferredT: JQuery.Promise2 | - JQuery.Promise3, - deferredU: JQuery.Promise2 | - JQuery.Promise3, - deferredV: JQuery.Promise2 | - JQuery.Promise3): JQuery.Promise3<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], [TN1, TN2, TN3], - [UR1, UR2, UR3], [UJ1, UJ2, UJ3], [UN1, UN2, UN3], - [VR1, VR2, VR3], [VJ1, VJ2, VJ3], [VN1, VN2, VN3]>; + (deferredT: JQuery.Promise3 | + JQuery.Promise2, + deferredU: JQuery.Promise3 | + JQuery.Promise2, + deferredV: JQuery.Promise3 | + JQuery.Promise2): JQuery.Promise3<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, + [UR1, UR2, UR3], [UJ1, UJ2, UJ3], never, + [VR1, VR2, VR3], [VJ1, VJ2, VJ3], never>; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3309,8 +3311,9 @@ interface JQueryStatic { */ when - (deferredT: TR1 | JQuery.Thenable | JQuery.Promise, - deferredU: UR1 | JQuery.Thenable | JQuery.Promise): JQuery.Promise2; + (deferredT: JQuery.Promise | JQuery.Thenable | TR1, + deferredU: JQuery.Promise | JQuery.Thenable | UR1): JQuery.Promise2; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3324,11 +3327,11 @@ interface JQueryStatic { UR1 = never, UJ1 = never, UN1 = never, UR2 = never, UJ2 = never, UN2 = never, UR3 = never, UJ3 = never, UN3 = never> - (deferredT: JQuery.Promise2 | - JQuery.Promise3, - deferredU: JQuery.Promise2 | - JQuery.Promise3): JQuery.Promise2<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], [TN1, TN2, TN3], - [UR1, UR2, UR3], [UJ1, UJ2, UJ3], [UN1, UN2, UN3]>; + (deferredT: JQuery.Promise3 | + JQuery.Promise2, + deferredU: JQuery.Promise3 | + JQuery.Promise2): JQuery.Promise2<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, + [UR1, UR2, UR3], [UJ1, UJ2, UJ3], never>; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3336,7 +3339,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(deferred: TR1 | JQuery.Thenable | JQuery.Promise): JQuery.Promise; + when(deferred: JQuery.Promise | JQuery.Thenable | TR1): JQuery.Promise; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3346,18 +3349,9 @@ interface JQueryStatic { */ when - (deferredT: JQuery.Promise3): JQuery.Promise3; - /** - * Provides a way to execute callback functions based on zero or more Thenable objects, usually - * Deferred objects that represent asynchronous events. - * - * @see {@link https://api.jquery.com/jQuery.when/} - * @since 1.5 - */ - when - (deferredT: JQuery.Promise2): JQuery.Promise2; + TR3 = never, TJ3 = never, TN3 = never> + (deferredT: JQuery.Promise3 | + JQuery.Promise2): JQuery.Promise3; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3366,7 +3360,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(...deferreds: Array>): JQuery.Promise; + when(...deferreds: Array>): JQuery.Promise; } declare namespace JQuery { diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 8a6710a2fd..609f65c6f1 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4449,27 +4449,51 @@ function JQueryStatic() { const w = $.when(t, u, v); w.then((a, b, c) => { - // $ExpectType [string, SuccessTextStatus, jqXHR] - a; - // $ExpectType [number, SuccessTextStatus, jqXHR] - b; - // $ExpectType [boolean, SuccessTextStatus, jqXHR] - c; + // $ExpectType string + a[0]; + // $ExpectType SuccessTextStatus + a[1]; + // $ExpectType jqXHR + a[2]; + // $ExpectType number + b[0]; + // $ExpectType SuccessTextStatus + b[1]; + // $ExpectType jqXHR + b[2]; + // $ExpectType boolean + c[0]; + // $ExpectType SuccessTextStatus + c[1]; + // $ExpectType jqXHR + c[2]; }); w.catch((a, b, c) => { - // $ExpectType [jqXHR, ErrorTextStatus, string] - a; - // $ExpectType [jqXHR, ErrorTextStatus, string] - b; - // $ExpectType [jqXHR, ErrorTextStatus, string] - c; + // $ExpectType jqXHR + a[0]; + // $ExpectType ErrorTextStatus + a[1]; + // $ExpectType string + a[2]; + // $ExpectType jqXHR + b[0]; + // $ExpectType ErrorTextStatus + b[1]; + // $ExpectType string + b[2]; + // $ExpectType jqXHR + c[0]; + // $ExpectType ErrorTextStatus + c[1]; + // $ExpectType string + c[2]; }); w.then(null, null, (a, b, c) => { - // $ExpectType [never, never, never] + // $ExpectType never a; - // $ExpectType [never, never, never] + // $ExpectType never b; - // $ExpectType [never, never, never] + // $ExpectType never c; }); } @@ -4479,21 +4503,37 @@ function JQueryStatic() { const w = $.when(t, u); w.then((a, b) => { - // $ExpectType [string, SuccessTextStatus, jqXHR] - a; - // $ExpectType [number, SuccessTextStatus, jqXHR] - b; + // $ExpectType string + a[0]; + // $ExpectType SuccessTextStatus + a[1]; + // $ExpectType jqXHR + a[2]; + // $ExpectType number + b[0]; + // $ExpectType SuccessTextStatus + b[1]; + // $ExpectType jqXHR + b[2]; }); w.catch((a, b) => { - // $ExpectType [jqXHR, ErrorTextStatus, string] - a; - // $ExpectType [jqXHR, ErrorTextStatus, string] - b; + // $ExpectType jqXHR + a[0]; + // $ExpectType ErrorTextStatus + a[1]; + // $ExpectType string + a[2]; + // $ExpectType jqXHR + b[0]; + // $ExpectType ErrorTextStatus + b[1]; + // $ExpectType string + b[2]; }); w.then(null, null, (a, b) => { - // $ExpectType [never, never, never] + // $ExpectType never a; - // $ExpectType [never, never, never] + // $ExpectType never b; }); } @@ -4539,27 +4579,39 @@ function JQueryStatic() { const w = $.when(t, u, v); w.then((a, b, c) => { - // $ExpectType [string, boolean, never] - a; - // $ExpectType [string, boolean, never] - b; - // $ExpectType [string, boolean, never] - c; + // $ExpectType string + a[0]; + // $ExpectType boolean + a[1]; + // $ExpectType string + b[0]; + // $ExpectType boolean + b[1]; + // $ExpectType string + c[0]; + // $ExpectType boolean + c[1]; }); w.catch((a, b, c) => { - // $ExpectType [Error, any, never] - a; - // $ExpectType [Error, any, never] - b; - // $ExpectType [Error, any, never] - c; + // $ExpectType Error + a[0]; + // $ExpectType any + a[1]; + // $ExpectType Error + b[0]; + // $ExpectType any + b[1]; + // $ExpectType Error + c[0]; + // $ExpectType any + c[1]; }); w.then(null, null, (a, b, c) => { - // $ExpectType [number, any, never] + // $ExpectType never a; - // $ExpectType [number, any, never] + // $ExpectType never b; - // $ExpectType [number, any, never] + // $ExpectType never c; }); } @@ -4569,21 +4621,29 @@ function JQueryStatic() { const w = $.when(t, u); w.then((a, b) => { - // $ExpectType [string, boolean, never] - a; - // $ExpectType [string, boolean, never] - b; + // $ExpectType string + a[0]; + // $ExpectType boolean + a[1]; + // $ExpectType string + b[0]; + // $ExpectType boolean + b[1]; }); w.catch((a, b) => { - // $ExpectType [Error, any, never] - a; - // $ExpectType [Error, any, never] - b; + // $ExpectType Error + a[0]; + // $ExpectType any + a[1]; + // $ExpectType Error + b[0]; + // $ExpectType any + b[1]; }); w.then(null, null, (a, b) => { - // $ExpectType [number, any, never] + // $ExpectType never a; - // $ExpectType [number, any, never] + // $ExpectType never b; }); } @@ -4605,9 +4665,9 @@ function JQueryStatic() { b; }); w.then(null, null, (a, b) => { - // $ExpectType number + // $ExpectType never a; - // $ExpectType any + // $ExpectType never b; }); } @@ -4620,14 +4680,12 @@ function JQueryStatic() { // $ExpectType string value; }); - w.catch(reason => { // $ExpectType Error reason; }); - w.then(null, null, value => { - // $ExpectType number + // $ExpectType never value; }); } @@ -4676,11 +4734,7 @@ function JQueryStatic() { } $.when().then(() => { - const f = first(); - // $ExpectType Promise - f; - - return f; + return first(); }).then((value) => { // $ExpectType string value; @@ -4699,12 +4753,18 @@ function JQueryStatic() { const task2 = this.runTask2(); const task3 = this.runTask3(); - // $ExpectType Promise - task1; - // $ExpectType Promise - task2; - // $ExpectType Promise - task3; + task1.then(value => { + // $ExpectType One + value; + }); + task2.then(value => { + // $ExpectType Two + value; + }); + task3.then(value => { + // $ExpectType Three + value; + }); $.when(task1, task2, task3) .done((r1, r2, r3) => { From f3bcbe6132ecc5042804db7ccfa8911f7e5cef0e Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 10:39:41 -0400 Subject: [PATCH 16/35] [jquery] Document 3.2 deprecations. --- types/jquery/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index bae24d9780..d13ed8af42 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2906,6 +2906,7 @@ interface JQueryStatic { * @param hold Indicates whether the ready hold is being requested or released * @see {@link https://api.jquery.com/jQuery.holdReady/} * @since 1.6 + * @deprecated 3.2 */ holdReady(hold: boolean): void; /** @@ -2932,6 +2933,7 @@ interface JQueryStatic { * @param obj Object to test whether or not it is an array. * @see {@link https://api.jquery.com/jQuery.isArray/} * @since 1.3 + * @deprecated 3.2 */ isArray(obj: any): obj is any[]; /** From c6ba2a627643107bb092c96eb44f71048eeb1cec Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 11:42:19 -0400 Subject: [PATCH 17/35] [jquery] Allow writing through index signature on JQuery. --- types/jquery/index.d.ts | 4 +++- types/jquery/jquery-tests.ts | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index d13ed8af42..8d688d1b20 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2390,9 +2390,11 @@ interface JQuery { * @since 1.4 */ wrapInner(wrappingElement: JQuery.Selector | JQuery.htmlString | Element | JQuery | ((this: TElement, index: number) => string | JQuery | Element)): this; + + [n: number]: TElement; } -interface JQuery extends ArrayLike, Iterable { } +interface JQuery extends Iterable { } interface JQueryStatic { /** diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 609f65c6f1..cacd81bcff 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -11,7 +11,10 @@ function JQuery() { } function arrayLike() { - $('div')[0] === new HTMLElement(); + // $ExpectType HTMLElement + $('div')[0]; + + $('div')[0] = new HTMLElement(); } function ajax() { From db67ce96bf7a591ff3b4a67aec3958cbefbd9483 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 12:38:05 -0400 Subject: [PATCH 18/35] [jquery] Promises cleanup. --- types/jquery/index.d.ts | 206 ++++++++++++++++++++-------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 8d688d1b20..22c8ccc40e 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3364,7 +3364,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(...deferreds: Array>): JQuery.Promise; + when(...deferreds: Array | JQuery.Thenable | TR1>): JQuery.Promise; } declare namespace JQuery { @@ -4318,14 +4318,14 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.promise/} * @since 1.5 */ - promise(target: TTarget): JQuery.Promise & TTarget; + promise(target: TTarget): JQuery.Promise & TTarget; /** * Return a Deferred's Promise object. * * @see {@link https://api.jquery.com/deferred.promise/} * @since 1.5 */ - promise(): JQuery.Promise; + promise(): JQuery.Promise; /** * Determine the current state of a Deferred object. * @@ -4341,7 +4341,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.catch/} * @since 3.0 */ - catch(failFilter: (...reasons: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + catch(failFilter: (...reasons: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. @@ -4355,9 +4355,9 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -4371,8 +4371,8 @@ declare namespace JQuery { */ pipe (doneFilter: null, - failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -4385,9 +4385,9 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, failFilter: null, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -4402,7 +4402,7 @@ declare namespace JQuery { pipe (doneFilter: null, failFilter: null, - progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -4414,8 +4414,8 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -4428,7 +4428,7 @@ declare namespace JQuery { */ pipe (doneFilter: null, - failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -4439,7 +4439,7 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -4451,9 +4451,9 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4465,8 +4465,8 @@ declare namespace JQuery { */ then (doneFilter: null, - failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4477,9 +4477,9 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, failFilter: null, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4492,7 +4492,7 @@ declare namespace JQuery { then (doneFilter: null, failFilter: null, - progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4502,8 +4502,8 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4514,7 +4514,7 @@ declare namespace JQuery { */ then (doneFilter: null, - failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4523,7 +4523,7 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Call the progressCallbacks on a Deferred object with the given args. @@ -4695,7 +4695,7 @@ declare namespace JQuery { catch - (failFilter: (t: TJ, u: UJ, v: VJ) => AR | Thenable | Promise3): Promise3; + (failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR): Promise3; /** * Utility method to filter and/or chain Deferreds. @@ -4711,9 +4711,9 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, - failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3, - progressFilter: (t: TN, u: UN, v: VN) => AN3 | Thenable | Promise3): Promise3 Promise3 | Thenable | AR1, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN3): Promise3; /** @@ -4731,8 +4731,8 @@ declare namespace JQuery { BR1 = never, BR2 = never, BJ1 = never, BJ2 = never, BN1 = BN2, BN2 = UN, CR1 = never, CR2 = never, CJ1 = never, CJ2 = never, CN1 = CN2, CN2 = VN> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => AR1 | Thenable | Promise3, - progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3 Promise3 | Thenable | AR1, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN1): Promise3; /** @@ -4749,9 +4749,9 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3 Promise3 | Thenable | AN1): Promise3; /** @@ -4770,7 +4770,7 @@ declare namespace JQuery { CR = VJ, CJ = VJ, CN = never> (doneFilter: null, failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => AN | Thenable | Promise3): Promise3; + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN): Promise3; /** * Utility method to filter and/or chain Deferreds. * @@ -4784,8 +4784,8 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, - failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3): Promise3 Promise3 | Thenable | AR1, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2): Promise3; /** @@ -4802,7 +4802,7 @@ declare namespace JQuery { BR = never, BJ = never, BN = UN, CR = never, CJ = never, CN = VN> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => AR | Thenable | Promise3): Promise3; + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR): Promise3; /** * Utility method to filter and/or chain Deferreds. * @@ -4815,7 +4815,7 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => AR | Thenable | Promise3): Promise3; + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR): Promise3; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -4829,9 +4829,9 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, - failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3, - progressFilter: (t: TN, u: UN, v: VN) => AN3 | Thenable | Promise3): Promise3 Promise3 | Thenable | AR1, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN3): Promise3; /** @@ -4847,8 +4847,8 @@ declare namespace JQuery { BR1 = never, BR2 = never, BJ1 = never, BJ2 = never, BN1 = BN2, BN2 = UN, CR1 = never, CR2 = never, CJ1 = never, CJ2 = never, CN1 = CN2, CN2 = VN> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => AR1 | Thenable | Promise3, - progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3 Promise3 | Thenable | AR1, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN1): Promise3; /** @@ -4863,9 +4863,9 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => AN1 | Thenable | Promise3): Promise3 Promise3 | Thenable | AN1): Promise3; /** @@ -4882,7 +4882,7 @@ declare namespace JQuery { CR = VJ, CJ = VJ, CN = never> (doneFilter: null, failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => AN | Thenable | Promise3): Promise3; + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN): Promise3; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4894,8 +4894,8 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => AR1 | Thenable | Promise3, - failFilter: (t: TJ, u: UJ, v: VJ) => AR2 | Thenable | Promise3): Promise3 Promise3 | Thenable | AR1, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2): Promise3; /** @@ -4910,7 +4910,7 @@ declare namespace JQuery { BR = never, BJ = never, BN = UN, CR = never, CJ = never, CN = VN> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => AR | Thenable | Promise3): Promise3; + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR): Promise3; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4921,7 +4921,7 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => AR | Thenable | Promise3): Promise3; + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR): Promise3; } /** @@ -5005,7 +5005,7 @@ declare namespace JQuery { */ catch - (failFilter: (t: TJ, u: UJ) => AR | Thenable | Promise2): Promise2; + (failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR): Promise2; /** * Utility method to filter and/or chain Deferreds. @@ -5020,9 +5020,9 @@ declare namespace JQuery { */ pipe - (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, - failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2, - progressFilter: (t: TN, u: UN) => AN3 | Thenable | Promise2): Promise2 Promise2 | Thenable | AR1, + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2, + progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN3): Promise2; /** * Utility method to filter and/or chain Deferreds. @@ -5038,8 +5038,8 @@ declare namespace JQuery { pipe (doneFilter: null, - failFilter: (t: TJ, u: UJ) => AR1 | Thenable | Promise2, - progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2 Promise2 | Thenable | AR1, + progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN1): Promise2; /** * Utility method to filter and/or chain Deferreds. @@ -5054,9 +5054,9 @@ declare namespace JQuery { */ pipe - (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, failFilter: null, - progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2 Promise2 | Thenable | AN1): Promise2; /** * Utility method to filter and/or chain Deferreds. @@ -5073,7 +5073,7 @@ declare namespace JQuery { BR = UJ, BJ = UJ, BN = never> (doneFilter: null, failFilter: null, - progressFilter: (t: TN, u: UN) => AN | Thenable | Promise2): Promise2; + progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN): Promise2; /** * Utility method to filter and/or chain Deferreds. * @@ -5086,8 +5086,8 @@ declare namespace JQuery { */ pipe - (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, - failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2): Promise2 Promise2 | Thenable | AR1, + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2): Promise2; /** * Utility method to filter and/or chain Deferreds. @@ -5102,7 +5102,7 @@ declare namespace JQuery { pipe (doneFilter: null, - failFilter: (t: TJ, u: UJ) => AR | Thenable | Promise2): Promise2; + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR): Promise2; /** * Utility method to filter and/or chain Deferreds. * @@ -5114,7 +5114,7 @@ declare namespace JQuery { */ pipe - (doneFilter: (t: TR, u: UR) => AR | Thenable | Promise2): Promise2; + (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5127,9 +5127,9 @@ declare namespace JQuery { */ then - (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, - failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2, - progressFilter: (t: TN, u: UN) => AN3 | Thenable | Promise2): Promise2 Promise2 | Thenable | AR1, + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2, + progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN3): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5143,8 +5143,8 @@ declare namespace JQuery { then (doneFilter: null, - failFilter: (t: TJ, u: UJ) => AR1 | Thenable | Promise2, - progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2 Promise2 | Thenable | AR1, + progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN1): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5157,9 +5157,9 @@ declare namespace JQuery { */ then - (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, + (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, failFilter: null, - progressFilter: (t: TN, u: UN) => AN1 | Thenable | Promise2): Promise2 Promise2 | Thenable | AN1): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5174,7 +5174,7 @@ declare namespace JQuery { BR = UJ, BJ = UJ, BN = never> (doneFilter: null, failFilter: null, - progressFilter: (t: TN, u: UN) => AN | Thenable | Promise2): Promise2; + progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5185,8 +5185,8 @@ declare namespace JQuery { */ then - (doneFilter: (t: TR, u: UR) => AR1 | Thenable | Promise2, - failFilter: (t: TJ, u: UJ) => AR2 | Thenable | Promise2): Promise2 Promise2 | Thenable | AR1, + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5199,7 +5199,7 @@ declare namespace JQuery { then (doneFilter: null, - failFilter: (t: TJ, u: UJ) => AR | Thenable | Promise2): Promise2; + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5209,7 +5209,7 @@ declare namespace JQuery { */ then - (doneFilter: (t: TR, u: UR) => AR | Thenable | Promise2): Promise2; + (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR): Promise2; } /** @@ -5290,7 +5290,7 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.catch/} * @since 3.0 */ - catch(failFilter: (...reasons: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + catch(failFilter: (...reasons: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. @@ -5304,9 +5304,9 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5320,8 +5320,8 @@ declare namespace JQuery { */ pipe (doneFilter: null, - failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5334,9 +5334,9 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, failFilter: null, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5351,7 +5351,7 @@ declare namespace JQuery { pipe (doneFilter: null, failFilter: null, - progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5363,8 +5363,8 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5377,7 +5377,7 @@ declare namespace JQuery { */ pipe (doneFilter: null, - failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5388,7 +5388,7 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5400,9 +5400,9 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN3 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5414,8 +5414,8 @@ declare namespace JQuery { */ then (doneFilter: null, - failFilter: (...t: TJ[]) => AR1 | Thenable | JQuery.Promise, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5426,9 +5426,9 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, failFilter: null, - progressFilter: (...t: TN[]) => AN1 | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5441,7 +5441,7 @@ declare namespace JQuery { then (doneFilter: null, failFilter: null, - progressFilter: (...t: TN[]) => AN | Thenable | JQuery.Promise): JQuery.Promise; + progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5451,8 +5451,8 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR1 | Thenable | JQuery.Promise, - failFilter: (...t: TJ[]) => AR2 | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5463,7 +5463,7 @@ declare namespace JQuery { */ then (doneFilter: null, - failFilter: (...t: TJ[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5472,7 +5472,7 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => AR | Thenable | JQuery.Promise): JQuery.Promise; + (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; } // endregion From 6de6e46771fdaab4350aeea58270f8b5a46ec075 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 12:48:40 -0400 Subject: [PATCH 19/35] [jquery] Re-add non-generic catch-all for when(). --- types/jquery/index.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 22c8ccc40e..1e0b8de12c 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3365,6 +3365,15 @@ interface JQueryStatic { * @since 1.5 */ when(...deferreds: Array | JQuery.Thenable | TR1>): JQuery.Promise; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @param deferreds Zero or more Thenable objects. + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when(...deferreds: any[]): JQuery.Promise; } declare namespace JQuery { From 6fa5e4bc987d947b14f26c3c7b4e631b1fe40cb1 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 12:57:00 -0400 Subject: [PATCH 20/35] [jquery] Make isEmptyObject() a user-defined type guard. --- types/jquery/index.d.ts | 2 +- types/jquery/jquery-tests.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 1e0b8de12c..6c9f2abe7b 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2945,7 +2945,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.isEmptyObject/} * @since 1.4 */ - isEmptyObject(obj: any): boolean; + isEmptyObject(obj: any): obj is {}; /** * Determine if the argument passed is a JavaScript function object. * diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index cacd81bcff..5487fb5b38 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4099,8 +4099,12 @@ function JQueryStatic() { } function isEmptyObject() { - // $ExpectType boolean - $.isEmptyObject({}); + function type_guard(obj: object) { + if ($.isEmptyObject(obj)) { + // $ExpectType {} + obj; + } + } } function isFunction() { From 706908910c4fff48bd40d08af499319bc55e85cc Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 13:02:05 -0400 Subject: [PATCH 21/35] [jquery] Tween cannot be a class under JQuery. --- types/jquery/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 6c9f2abe7b..ebedbe2530 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -5568,10 +5568,11 @@ declare namespace JQuery { complete?(this: TElement): void; } + // This should be a class but doesn't work correctly under the JQuery namespace. Tween should be an inner class of jQuery. // Undocumented // https://github.com/jquery/api.jquery.com/issues/391 // https://github.com/jquery/api.jquery.com/issues/61 - class Tween { + interface Tween { easing: string; elem: TElement; end: number; From a0426430da440683c328f9053d85d1b413d311b7 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Mon, 26 Jun 2017 17:22:06 -0400 Subject: [PATCH 22/35] [jquery] Clean up promises. --- types/jquery/index.d.ts | 143 +++++++----- types/jquery/jquery-tests.ts | 421 ++++++++++++++++++++++++++++++++--- 2 files changed, 474 insertions(+), 90 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index ebedbe2530..278cc38d82 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3274,12 +3274,11 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when - (deferredT: JQuery.Promise | JQuery.Thenable | TR1, - deferredU: JQuery.Promise | JQuery.Thenable | UR1, - deferredV: JQuery.Promise | JQuery.Thenable | VR1): JQuery.Promise3 + (deferredT: JQuery.Promise | JQuery.Thenable | TR1, + deferredU: JQuery.Promise | JQuery.Thenable | UR1, + deferredV: JQuery.Promise | JQuery.Thenable | VR1): JQuery.Promise3; /** @@ -3289,21 +3288,21 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when - (deferredT: JQuery.Promise3 | - JQuery.Promise2, - deferredU: JQuery.Promise3 | - JQuery.Promise2, - deferredV: JQuery.Promise3 | - JQuery.Promise2): JQuery.Promise3<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, + when + (deferredT: JQuery.Promise3 | + JQuery.Promise2, + deferredU: JQuery.Promise3 | + JQuery.Promise2, + deferredV: JQuery.Promise3 | + JQuery.Promise2): JQuery.Promise3<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, [UR1, UR2, UR3], [UJ1, UJ2, UJ3], never, [VR1, VR2, VR3], [VJ1, VJ2, VJ3], never>; /** @@ -3313,10 +3312,10 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when - (deferredT: JQuery.Promise | JQuery.Thenable | TR1, - deferredU: JQuery.Promise | JQuery.Thenable | UR1): JQuery.Promise2 + (deferredT: JQuery.Promise | JQuery.Thenable | TR1, + deferredU: JQuery.Promise | JQuery.Thenable | UR1): JQuery.Promise2; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually @@ -3325,16 +3324,16 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when - (deferredT: JQuery.Promise3 | - JQuery.Promise2, - deferredU: JQuery.Promise3 | - JQuery.Promise2): JQuery.Promise2<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, + when + (deferredT: JQuery.Promise3 | + JQuery.Promise2, + deferredU: JQuery.Promise3 | + JQuery.Promise2): JQuery.Promise2<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, [UR1, UR2, UR3], [UJ1, UJ2, UJ3], never>; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually @@ -3343,7 +3342,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(deferred: JQuery.Promise | JQuery.Thenable | TR1): JQuery.Promise; + when(deferred: JQuery.Promise | JQuery.Thenable | TR1): JQuery.Promise; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3351,11 +3350,11 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when - (deferredT: JQuery.Promise3 | - JQuery.Promise2): JQuery.Promise3; + when + (deferredT: JQuery.Promise3 | + JQuery.Promise2): JQuery.Promise3; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3364,7 +3363,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(...deferreds: Array | JQuery.Thenable | TR1>): JQuery.Promise; + when(...deferreds: Array | JQuery.Thenable | TR1>): JQuery.Promise; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -4794,7 +4793,8 @@ declare namespace JQuery { BR1 = never, BR2 = never, BJ1 = never, BJ2 = never, BN1 = UN, BN2 = UN, CR1 = never, CR2 = never, CJ1 = never, CJ2 = never, CN1 = VN, CN2 = VN> (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2): Promise3 Promise3 | Thenable | AR2, + progressFilter?: null): Promise3; /** @@ -4811,7 +4811,8 @@ declare namespace JQuery { BR = never, BJ = never, BN = UN, CR = never, CJ = never, CN = VN> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR): Promise3; + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR, + progressFilter?: null): Promise3; /** * Utility method to filter and/or chain Deferreds. * @@ -4824,7 +4825,9 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR): Promise3; + (doneFilter: ((t: TR, u: UR, v: VR) => Promise3 | Thenable | AR) | null, + failFilter?: null, + progressFilter?: null): Promise3; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -4904,7 +4907,8 @@ declare namespace JQuery { BR1 = never, BR2 = never, BJ1 = never, BJ2 = never, BN1 = UN, BN2 = UN, CR1 = never, CR2 = never, CJ1 = never, CJ2 = never, CN1 = VN, CN2 = VN> (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2): Promise3 Promise3 | Thenable | AR2, + progressFilter?: null): Promise3; /** @@ -4919,7 +4923,8 @@ declare namespace JQuery { BR = never, BJ = never, BN = UN, CR = never, CJ = never, CN = VN> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR): Promise3; + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR, + progressFilter?: null): Promise3; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -4930,7 +4935,9 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR): Promise3; + (doneFilter: ((t: TR, u: UR, v: VR) => Promise3 | Thenable | AR) | null, + failFilter?: null, + progressFilter?: null): Promise3; } /** @@ -5096,7 +5103,8 @@ declare namespace JQuery { pipe (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2): Promise2 Promise2 | Thenable | AR2, + progressFilter?: null): Promise2; /** * Utility method to filter and/or chain Deferreds. @@ -5111,7 +5119,8 @@ declare namespace JQuery { pipe (doneFilter: null, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR): Promise2; + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR, + progressFilter?: null): Promise2; /** * Utility method to filter and/or chain Deferreds. * @@ -5123,7 +5132,9 @@ declare namespace JQuery { */ pipe - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR): Promise2; + (doneFilter: ((t: TR, u: UR) => Promise2 | Thenable | AR) | null, + failFilter?: null, + progressFilter?: null): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5195,7 +5206,8 @@ declare namespace JQuery { then (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2): Promise2 Promise2 | Thenable | AR2, + progressFilter?: null): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5208,7 +5220,8 @@ declare namespace JQuery { then (doneFilter: null, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR): Promise2; + failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR, + progressFilter?: null): Promise2; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5218,7 +5231,9 @@ declare namespace JQuery { */ then - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR): Promise2; + (doneFilter: ((t: TR, u: UR) => Promise2 | Thenable | AR) | null, + failFilter?: null, + progressFilter?: null): Promise2; } /** @@ -5373,7 +5388,8 @@ declare namespace JQuery { */ pipe (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, + progressFilter?: null): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5386,7 +5402,8 @@ declare namespace JQuery { */ pipe (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR, + progressFilter?: null): JQuery.Promise; /** * Utility method to filter and/or chain Deferreds. * @@ -5397,7 +5414,9 @@ declare namespace JQuery { * @deprecated 1.8 */ pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; + (doneFilter: ((...t: TR[]) => JQuery.Promise | Thenable | AR) | null, + failFilter?: null, + progressFilter?: null): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -5461,7 +5480,8 @@ declare namespace JQuery { */ then (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, + progressFilter?: null): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5472,7 +5492,8 @@ declare namespace JQuery { */ then (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; + failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR, + progressFilter?: null): JQuery.Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. * @@ -5481,7 +5502,9 @@ declare namespace JQuery { * @since 1.8 */ then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; + (doneFilter: ((...t: TR[]) => JQuery.Promise | Thenable | AR) | null, + failFilter?: null, + progressFilter?: null): JQuery.Promise; } // endregion diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 5487fb5b38..6638f18c89 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -5400,51 +5400,161 @@ function jqXHR() { } } -function Promise3() { +function Promise3(p: JQuery.Promise3) { function then() { + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }, (a, b, c) => { + a; // $ExpectType number + b; // $ExpectType boolean + c; // $ExpectType never + }); + + p.then(null, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }, (a, b, c) => { + a; // $ExpectType number + b; // $ExpectType boolean + c; // $ExpectType never + }); + + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, null, (a, b, c) => { + a; // $ExpectType number + b; // $ExpectType boolean + c; // $ExpectType never + }); + + p.then(null, null, (a, b, c) => { + a; // $ExpectType number + b; // $ExpectType boolean + c; // $ExpectType never + }); + + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }); + + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }, null); + + p.then(null, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }); + + p.then(null, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }, null); + + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }); + + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, null); + + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, null, null); + + p.then(null); + + p.then(null, null); + + p.then(null, null, null); + function doneFilter() { - // $ExpectType Promise3, never, never, ErrorTextStatus, never, never, string, never> - $.ajax('/echo/json').then(() => { + p.then(() => { return 1; + }).then((a) => { + a; // $ExpectType number }); - // $ExpectType Promise3, never, never, ErrorTextStatus, never, never, string, never> - $.ajax('/echo/json').then(() => { - const t: JQuery.Thenable = { - then() { - return Promise.resolve('myValue'); - } - }; - - return t; + p.then(() => { + return $.ready; + }).then((a) => { + a; // $ExpectType JQueryStatic }); - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(() => { - return $.ajax('/echo/json'); + p.then(() => { + return p; + }).then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }, (a, b, c) => { + a; // $ExpectType number + b; // $ExpectType boolean + c; // $ExpectType never }); } - function null_failFilter() { - // $ExpectType Promise3 - $.ajax('/echo/json').then(null, () => { + function failFilter() { + p.then(null, () => { return 1; + }).then((a) => { + a; // $ExpectType number }); - // $ExpectType Promise3 - $.ajax('/echo/json').then(null, () => { - const t: JQuery.Thenable = { - then() { - return Promise.resolve('myValue'); - } - }; - - return t; + p.then(null, () => { + return $.ready; + }).then((a) => { + a; // $ExpectType JQueryStatic }); - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(null, () => { - return $.ajax('/echo/json'); + p.then(null, () => { + return p; + }).then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function + }, (a, b, c) => { + a; // $ExpectType number + b; // $ExpectType boolean + c; // $ExpectType never }); } @@ -5517,3 +5627,254 @@ function Promise3() { }); } } + +function Promise2(p: JQuery.Promise2) { + function then() { + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }, (a, b) => { + a; // $ExpectType number + b; // $ExpectType boolean + }); + + p.then(null, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }, (a, b) => { + a; // $ExpectType number + b; // $ExpectType boolean + }); + + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, null, (a, b) => { + a; // $ExpectType number + b; // $ExpectType boolean + }); + + p.then(null, null, (a, b) => { + a; // $ExpectType number + b; // $ExpectType boolean + }); + + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }); + + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }, null); + + p.then(null, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }); + + p.then(null, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }, null); + + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }); + + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, null); + + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, null, null); + + p.then(null); + + p.then(null, null); + + p.then(null, null, null); + + function doneFilter() { + p.then(() => { + return 1; + }).then((a) => { + a; // $ExpectType number + }); + + p.then(() => { + return $.ready; + }).then((a) => { + a; // $ExpectType JQueryStatic + }); + + p.then(() => { + return p; + }).then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }, (a, b) => { + a; // $ExpectType number + b; // $ExpectType boolean + }); + } + + function failFilter() { + p.then(null, () => { + return 1; + }).then((a) => { + a; // $ExpectType number + }); + + p.then(null, () => { + return $.ready; + }).then((a) => { + a; // $ExpectType JQueryStatic + }); + + p.then(null, () => { + return p; + }).then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string + }, (a, b) => { + a; // $ExpectType number + b; // $ExpectType boolean + }); + } + } +} + +function _Promise(p: JQuery.Promise) { + function then() { + p.then((a) => { + a; // $ExpectType string + }, (a) => { + a; // $ExpectType Error + }, (a) => { + a; // $ExpectType number + }); + + p.then(null, (a) => { + a; // $ExpectType Error + }, (a) => { + a; // $ExpectType number + }); + + p.then((a) => { + a; // $ExpectType string + }, null, (a) => { + a; // $ExpectType number + }); + + p.then(null, null, (a) => { + a; // $ExpectType number + }); + + p.then((a) => { + a; // $ExpectType string + }, (a) => { + a; // $ExpectType Error + }); + + p.then((a) => { + a; // $ExpectType string + }, (a) => { + a; // $ExpectType Error + }, null); + + p.then(null, (a) => { + a; // $ExpectType Error + }); + + p.then(null, (a) => { + a; // $ExpectType Error + }, null); + + p.then((a) => { + a; // $ExpectType string + }); + + p.then((a) => { + a; // $ExpectType string + }, null); + + p.then((a) => { + a; // $ExpectType string + }, null, null); + + p.then(null); + + p.then(null, null); + + p.then(null, null, null); + + function doneFilter() { + p.then(() => { + return 1; + }).then((a) => { + a; // $ExpectType number + }); + + p.then(() => { + return $.ready; + }).then((a) => { + a; // $ExpectType JQueryStatic + }); + + p.then(() => { + return p; + }).then((a) => { + a; // $ExpectType string + }, (a) => { + a; // $ExpectType Error + }, (a) => { + a; // $ExpectType number + }); + } + + function failFilter() { + p.then(null, () => { + return 1; + }).then((a) => { + a; // $ExpectType number + }); + + p.then(null, () => { + return $.ready; + }).then((a) => { + a; // $ExpectType JQueryStatic + }); + + p.then(null, () => { + return p; + }).then((a) => { + a; // $ExpectType string + }, (a) => { + a; // $ExpectType Error + }, (a) => { + a; // $ExpectType number + }); + } + } +} From 11f6048075f6bc7fd770a6cfbb9d29401af1679c Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 05:58:11 -0400 Subject: [PATCH 23/35] [jquery] Fix typo that broke a bunch of tests. --- types/jquery/jquery-tests.ts | 86 ++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index f34cc443a5..b4b4f10aaf 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -3482,10 +3482,10 @@ function JQueryStatic() { } function call_signature() { - // #ExpectType JQuery + // $ExpectType JQuery $('

', new Document()); - // #ExpectType JQuery + // $ExpectType JQuery $('

', { class: 'my-div', on: { @@ -3495,149 +3495,149 @@ function JQueryStatic() { } }); - // #ExpectType JQuery + // $ExpectType JQuery $('span', new HTMLElement()); - // #ExpectType JQuery + // $ExpectType JQuery $('span', new Document()); - // #ExpectType JQuery + // $ExpectType JQuery $('span', $('p')); - // #ExpectType JQuery + // $ExpectType JQuery $('span'); - // #ExpectType JQuery + // $ExpectType JQuery $('

'); - // #ExpectType JQuery + // $ExpectType JQuery $(new HTMLElement()); - // #ExpectType JQuery + // $ExpectType JQuery $([new HTMLElement()]); - // #ExpectType JQuery + // $ExpectType JQuery $({ foo: 'bar', hello: 'world' }); - // #ExpectType JQuery + // $ExpectType JQuery $($('p')); - // #ExpectType JQuery + // $ExpectType JQuery $(function($) { - // #ExpectType Document + // $ExpectType Document this; - // #ExpectType JQueryStatic + // $ExpectType JQueryStatic $; }); - // #ExpectType JQuery + // $ExpectType JQuery $(); } function Event() { - // #ExpectType Event + // $ExpectType EventStatic $.Event; } function cssHooks() { - // #ExpectType PlainObject> + // $ExpectType PlainObject> $.cssHooks; } function cssNumber() { - // #ExpectType PlainObject + // $ExpectType PlainObject $.cssNumber; } function fn() { - // #ExpectType JQuery + // $ExpectType JQuery $.fn; } function fx() { function interval() { - // #ExpectType JQuery + // $ExpectType number $.fx.interval; } function off() { - // #ExpectType boolean + // $ExpectType boolean $.fx.off; } function step() { - // #ExpectType PlainObject> + // $ExpectType PlainObject> $.fx.step; } } function ready() { - // #ExpectType Thenable> + // $ExpectType Thenable> $.ready; } function support() { - // #ExpectType PlainObject + // $ExpectType PlainObject $.support; } function valHooks() { - // #ExpectType PlainObject> + // $ExpectType PlainObject> $.valHooks; } function Callbacks() { - // #ExpectType Callbacks + // $ExpectType Callbacks $.Callbacks('once'); - // #ExpectType Callbacks + // $ExpectType Callbacks $.Callbacks(); } function Deferred() { function call_signature() { - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(function(deferred) { - // #ExpectType Deferred + // $ExpectType Deferred this; - // #ExpectType Deferred + // $ExpectType Deferred deferred; }); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(function(deferred) { - // #ExpectType Deferred + // $ExpectType Deferred this; - // #ExpectType Deferred + // $ExpectType Deferred deferred; }); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(function(deferred) { - // #ExpectType Deferred + // $ExpectType Deferred this; - // #ExpectType Deferred + // $ExpectType Deferred deferred; }); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(function(deferred) { - // #ExpectType Deferred + // $ExpectType Deferred this; - // #ExpectType Deferred + // $ExpectType Deferred deferred; }); - // #ExpectType Deferred + // $ExpectType Deferred $.Deferred(); } From 8ec10d94be3a111df49764f69ee7c84cdbec16b6 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 08:12:06 -0400 Subject: [PATCH 24/35] [jquery] No null. --- types/jquery/index.d.ts | 2 +- types/jquery/jquery-tests.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index b4c4d37387..69196b6d00 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -2330,7 +2330,7 @@ interface JQuery { * @see {@link https://api.jquery.com/val/} * @since 1.0 */ - val(): string | number | string[] | null | undefined; + val(): string | number | string[] | undefined; /** * Set the CSS width of each element in the set of matched elements. * diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index b4b4f10aaf..7b0c521b88 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -3028,7 +3028,7 @@ function JQuery() { return 'myVal'; }); - // $ExpectType string | number | string[] | null | undefined + // $ExpectType string | number | string[] | undefined $('p').val(); } } From d2a6dc64c0fd75cdab1eff27d72170f86777a5c2 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 17:53:13 -0400 Subject: [PATCH 25/35] [jquery] Revise Promise3. --- types/jquery/index.d.ts | 590 +++++++++++++++++++++-------------- types/jquery/jquery-tests.ts | 364 ++++++++++++++------- 2 files changed, 599 insertions(+), 355 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 69196b6d00..e92560017d 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4620,6 +4620,19 @@ declare namespace JQuery { interface ProgressCallback extends Callback { } } + // Type parameter guide + // -------------------- + // Each type parameter represents a parameter in one of the three possible callbacks. + // + // The first letter indicates which position the parameter is in. + // + // T = A = 1st position + // U = B = 2nd position + // V = C = 3rd position + // + // The second letter indicates which whether it is a [R]esolve, Re[J]ect, or [N]otify value. + // + // The third letter indicates whether the value is returned in the [D]one filter, [F]ail filter, or [P]rogress filter. /** * This object provides a subset of the methods of the Deferred object (then, done, fail, always, * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. @@ -4628,7 +4641,7 @@ declare namespace JQuery { */ interface Promise3 { + VR, VJ, VN> { /** * Add handlers to be called when the Deferred object is either resolved or rejected. * @@ -4693,6 +4706,335 @@ declare namespace JQuery { */ state(): 'pending' | 'resolved' | 'rejected'; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AJF, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AJF, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AJF, + progressFilter?: null): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AJF, + progressFilter?: null): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AJF, + progressFilter?: null): Promise3; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter?: null, + progressFilter?: null): Promise3; + + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARF, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARF, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: null, + progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ANP): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARF, + progressFilter?: null): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARF, + progressFilter?: null): Promise3; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | ARD, + failFilter?: null, + progressFilter?: null): Promise3; + /** * Add handlers to be called when the Deferred object is rejected. * @@ -4700,244 +5042,14 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.catch/} * @since 3.0 */ - catch - (failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR): Promise3; - - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN3): Promise3; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR1, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN1): Promise3; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN1): Promise3; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN): Promise3; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2, - progressFilter?: null): Promise3; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR, - progressFilter?: null): Promise3; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: ((t: TR, u: UR, v: VR) => Promise3 | Thenable | AR) | null, - failFilter?: null, - progressFilter?: null): Promise3; - - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN3): Promise3; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR1, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN1): Promise3; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN1): Promise3; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AN): Promise3; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 | Thenable | AR1, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR2, - progressFilter?: null): Promise3; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | AR, - progressFilter?: null): Promise3; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: ((t: TR, u: UR, v: VR) => Promise3 | Thenable | AR) | null, - failFilter?: null, - progressFilter?: null): Promise3; + catch + (failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARF): Promise3; } /** diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 7b0c521b88..93a147bbfe 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -1,3 +1,5 @@ +// tslint:disable:interface-name + function JQuery() { function type_assertion() { const $el = $(document.createElement('canvas')); @@ -4586,40 +4588,25 @@ function JQueryStatic() { const w = $.when(t, u, v); w.then((a, b, c) => { - // $ExpectType string - a[0]; - // $ExpectType boolean - a[1]; - // $ExpectType string - b[0]; - // $ExpectType boolean - b[1]; - // $ExpectType string - c[0]; - // $ExpectType boolean - c[1]; + a[0]; // $ExpectType string + a[1]; // $ExpectType boolean + b[0]; // $ExpectType string + b[1]; // $ExpectType boolean + c[0]; // $ExpectType string + c[1]; // $ExpectType boolean }); w.catch((a, b, c) => { - // $ExpectType Error - a[0]; - // $ExpectType any - a[1]; - // $ExpectType Error - b[0]; - // $ExpectType any - b[1]; - // $ExpectType Error - c[0]; - // $ExpectType any - c[1]; + a[0]; // $ExpectType Error + a[1]; // $ExpectType any + b[0]; // $ExpectType Error + b[1]; // $ExpectType any + c[0]; // $ExpectType Error + c[1]; // $ExpectType any }); w.then(null, null, (a, b, c) => { - // $ExpectType never - a; - // $ExpectType never - b; - // $ExpectType never - c; + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never }); } @@ -4628,30 +4615,20 @@ function JQueryStatic() { const w = $.when(t, u); w.then((a, b) => { - // $ExpectType string - a[0]; - // $ExpectType boolean - a[1]; - // $ExpectType string - b[0]; - // $ExpectType boolean - b[1]; + a[0]; // $ExpectType string + a[1]; // $ExpectType boolean + b[0]; // $ExpectType string + b[1]; // $ExpectType boolean }); w.catch((a, b) => { - // $ExpectType Error - a[0]; - // $ExpectType any - a[1]; - // $ExpectType Error - b[0]; - // $ExpectType any - b[1]; + a[0]; // $ExpectType Error + a[1]; // $ExpectType any + b[0]; // $ExpectType Error + b[1]; // $ExpectType any }); w.then(null, null, (a, b) => { - // $ExpectType never - a; - // $ExpectType never - b; + a; // $ExpectType never + b; // $ExpectType never }); } @@ -4660,22 +4637,16 @@ function JQueryStatic() { const w = $.when(t); w.then((a, b) => { - // $ExpectType string - a; - // $ExpectType boolean - b; + a; // $ExpectType string + b; // $ExpectType boolean }); w.catch((a, b) => { - // $ExpectType Error - a; - // $ExpectType any - b; + a; // $ExpectType Error + b; // $ExpectType any }); w.then(null, null, (a, b) => { - // $ExpectType never - a; - // $ExpectType never - b; + a; // $ExpectType never + b; // $ExpectType never }); } } @@ -5400,7 +5371,26 @@ function jqXHR() { } } -function Promise3(p: JQuery.Promise3) { +function Promise3() { + interface I1 { kind: 'I1'; } + interface I2 { kind: 'I2'; } + interface I3 { kind: 'I3'; } + interface I4 { kind: 'I4'; } + interface I5 { kind: 'I5'; } + interface I6 { kind: 'I6'; } + interface I7 { kind: 'I7'; } + interface I8 { kind: 'I8'; } + interface I9 { kind: 'I9'; } + + const p: JQuery.Promise3 = {} as any; + const p1: JQuery.Promise3 = {} as any; + const p2: JQuery.Promise3 = {} as any; + const p3: JQuery.Promise3 = {} as any; + + const t1: JQuery.Thenable = {} as any; + const t2: JQuery.Thenable = {} as any; + const t3: JQuery.Thenable = {} as any; + function then() { p.then((a, b, c) => { a; // $ExpectType string @@ -5450,7 +5440,7 @@ function Promise3(p: JQuery.Promise3 { a; // $ExpectType string @@ -5460,12 +5450,6 @@ function Promise3(p: JQuery.Promise3 { - a; // $ExpectType Error - b; // $ExpectType string - c; // $ExpectType Function }); p.then(null, (a, b, c) => { @@ -5474,29 +5458,29 @@ function Promise3(p: JQuery.Promise3 { - a; // $ExpectType string - b; // $ExpectType JQuery - c; // $ExpectType any + p.then(null, (a, b, c) => { + a; // $ExpectType Error + b; // $ExpectType string + c; // $ExpectType Function }); - p.then((a, b, c) => { - a; // $ExpectType string - b; // $ExpectType JQuery - c; // $ExpectType any - }, null); - p.then((a, b, c) => { a; // $ExpectType string b; // $ExpectType JQuery c; // $ExpectType any }, null, null); - p.then(null); + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }, null); - p.then(null, null); - - p.then(null, null, null); + p.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType JQuery + c; // $ExpectType any + }); function doneFilter() { p.then(() => { @@ -5559,48 +5543,196 @@ function Promise3(p: JQuery.Promise3 { - return 1; - }; - const thenable = () => { - const t: JQuery.Thenable = { - then() { - return Promise.resolve('myValue'); - } - }; + // (value, value) + { + const q = p.then(() => { + return 1; + }, () => { + return 1; + }); - return t; - }; - const promise3 = () => { - return $.ajax('/echo/json') as JQuery.jqXHR; - }; + q.then((a) => { + a; // $ExpectType number + }, (a) => { + a; // $ExpectType never + }, (a, b, c) => { + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never + }); + } - // $ExpectType Promise3 - $.ajax('/echo/json').then(value, value); + // (Thenable, Thenable) + { + const q = p.then(() => { + return t1; + }, () => { + return t2; + }); - // $ExpectType Promise3 - $.ajax('/echo/json').then(thenable, thenable); + q.then((a) => { + a; // $ExpectType I1 | I2 + }, (a) => { + a; // $ExpectType never + }, (a, b, c) => { + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never + }); + } - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(promise3, promise3); + // (Promise3, Promise3) + { + const q = p.then(() => { + return p1; + }, () => { + return p2; + }); - // $ExpectType Promise3 - $.ajax('/echo/json').then(value, thenable); + q.then((a, b, c) => { + a; // $ExpectType I1 | I2 + b; // $ExpectType I4 | I5 + c; // $ExpectType I7 | I8 + }, (a, b, c) => { + a; // $ExpectType I2 | I3 + b; // $ExpectType I5 | I6 + c; // $ExpectType I8 | I9 + }, (a, b, c) => { + a; // $ExpectType I3 | I4 + b; // $ExpectType I6 | I7 + c; // $ExpectType I1 | I9 + }); + } - // $ExpectType Promise3 - $.ajax('/echo/json').then(thenable, value); + // (value, Thenable) + { + const q = p.then(() => { + return 1; + }, () => { + return t1; + }); - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(value, promise3); + q.then((a) => { + a; // $ExpectType number | I1 + }, (a) => { + a; // never + }, (a, b, c) => { + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never + }); + } - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(promise3, value); + // (Thenable, value) + { + const q = p.then(() => { + return t1; + }, () => { + return 1; + }); - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(thenable, promise3); + q.then((a) => { + a; // $ExpectType number | I1 + }, (a) => { + a; // never + }, (a, b, c) => { + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never + }); + } - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').then(promise3, thenable); + // (value, Promise3) + { + const q = p.then(() => { + return 1; + }, () => { + return p1; + }); + + q.then((a, b, c) => { + a; // $ExpectType number | I1 + b; // $ExpectType I4 + c; // $ExpectType I7 + }, (a, b, c) => { + a; // $ExpectType I2 + b; // $ExpectType I5 + c; // $ExpectType I8 + }, (a, b, c) => { + a; // $ExpectType I3 + b; // $ExpectType I6 + c; // $ExpectType I9 + }); + } + + // (Promise3, value) + { + const q = p.then(() => { + return p1; + }, () => { + return 1; + }); + + q.then((a, b, c) => { + a; // $ExpectType number | I1 + b; // $ExpectType I4 + c; // $ExpectType I7 + }, (a, b, c) => { + a; // $ExpectType I2 + b; // $ExpectType I5 + c; // $ExpectType I8 + }, (a, b, c) => { + a; // $ExpectType I3 + b; // $ExpectType I6 + c; // $ExpectType I9 + }); + } + + // (Thenable, Promise3) + { + const q = p.then(() => { + return t1; + }, () => { + return p2; + }); + + q.then((a, b, c) => { + a; // $ExpectType I1 | I2 + b; // $ExpectType I5 + c; // $ExpectType I8 + }, (a, b, c) => { + a; // $ExpectType I3 + b; // $ExpectType I6 + c; // $ExpectType I9 + }, (a, b, c) => { + a; // $ExpectType I4 + b; // $ExpectType I7 + c; // $ExpectType I1 + }); + } + + // (Promise3, Thenable) + { + const q = p.then(() => { + return p1; + }, () => { + return t2; + }); + + q.then((a, b, c) => { + a; // $ExpectType I1 | I2 + b; // $ExpectType I4 + c; // $ExpectType I7 + }, (a, b, c) => { + a; // $ExpectType I2 + b; // $ExpectType I5 + c; // $ExpectType I8 + }, (a, b, c) => { + a; // $ExpectType I3 + b; // $ExpectType I6 + c; // $ExpectType I9 + }); + } } } From d03b2b9ad5df3aecbead6a5ba573e0b1015d7486 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 17:57:35 -0400 Subject: [PATCH 26/35] [jquery] Change Promise2 to extend from Promise3. --- types/jquery/index.d.ts | 295 +---------------------------------- types/jquery/jquery-tests.ts | 41 ++--- 2 files changed, 18 insertions(+), 318 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index e92560017d..862cd57e4a 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4591,10 +4591,6 @@ declare namespace JQuery { (t: T, u: U, v: V): void; } - interface Callback2 { - (t: T, u: U): void; - } - interface Callback { (...args: T[]): void; } @@ -5059,294 +5055,9 @@ declare namespace JQuery { * @see {@link http://api.jquery.com/Types/#Promise} */ interface Promise2 { - /** - * Add handlers to be called when the Deferred object is either resolved or rejected. - * - * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. - * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. - * @see {@link https://api.jquery.com/deferred.always/} - * @since 1.6 - */ - always(alwaysCallback: TypeOrArray>, - ...alwaysCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is resolved. - * - * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. - * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.done/} - * @since 1.5 - */ - done(doneCallback: TypeOrArray>, - ...doneCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. - * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.fail/} - * @since 1.5 - */ - fail(failCallback: TypeOrArray>, - ...failCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object generates progress notifications. - * - * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. - * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates - * progress notifications. - * @see {@link https://api.jquery.com/deferred.progress/} - * @since 1.7 - */ - progress(progressCallback: TypeOrArray>, - ...progressCallbacks: Array>>): this; - /** - * Return a Deferred's Promise object. - * - * @param target Object onto which the promise methods have to be attached - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(target: TTarget): this & TTarget; - /** - * Return a Deferred's Promise object. - * - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(): this; - /** - * Determine the current state of a Deferred object. - * - * @see {@link https://api.jquery.com/deferred.state/} - * @since 1.7 - */ - state(): 'pending' | 'resolved' | 'rejected'; - - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failFilter A function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.catch/} - * @since 3.0 - */ - catch - (failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR): Promise2; - - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN3): Promise2; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR1, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN1): Promise2; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: null, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN1): Promise2; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: null, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN): Promise2; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2, - progressFilter?: null): Promise2; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR, - progressFilter?: null): Promise2; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: ((t: TR, u: UR) => Promise2 | Thenable | AR) | null, - failFilter?: null, - progressFilter?: null): Promise2; - - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN3): Promise2; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR1, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN1): Promise2; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: null, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN1): Promise2; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: null, - progressFilter: (t: TN, u: UN) => Promise2 | Thenable | AN): Promise2; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (t: TR, u: UR) => Promise2 | Thenable | AR1, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR2, - progressFilter?: null): Promise2; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (t: TJ, u: UJ) => Promise2 | Thenable | AR, - progressFilter?: null): Promise2; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: ((t: TR, u: UR) => Promise2 | Thenable | AR) | null, - failFilter?: null, - progressFilter?: null): Promise2; - } + UR, UJ, UN> extends Promise3 { } /** * This object provides a subset of the methods of the Deferred object (then, done, fail, always, diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 93a147bbfe..7347a8a415 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4579,9 +4579,9 @@ function JQueryStatic() { } function Promise2() { - const t = $.Deferred() as JQuery.Promise2; - const u = $.Deferred() as JQuery.Promise2; - const v = $.Deferred() as JQuery.Promise2; + const t = $.Deferred() as JQuery.Promise2; + const u = $.Deferred() as JQuery.Promise2; + const v = $.Deferred() as JQuery.Promise2; // 3 parameters { @@ -5364,11 +5364,6 @@ function jqXHR() { errorThrown; }); } - - function then_returnType() { - // $ExpectType Promise3, never, never, ErrorTextStatus, never, never, string, never> - $.ajax('/echo/json').then(() => { }); - } } function Promise3() { @@ -5800,7 +5795,7 @@ function Promise2(p: JQuery.Promise2 { a; // $ExpectType Error b; // $ExpectType string - }); + }, null); p.then((a, b) => { a; // $ExpectType string @@ -5808,11 +5803,6 @@ function Promise2(p: JQuery.Promise2 { a; // $ExpectType Error b; // $ExpectType string - }, null); - - p.then(null, (a, b) => { - a; // $ExpectType Error - b; // $ExpectType string }); p.then(null, (a, b) => { @@ -5820,26 +5810,25 @@ function Promise2(p: JQuery.Promise2 { - a; // $ExpectType string - b; // $ExpectType JQuery + p.then(null, (a, b) => { + a; // $ExpectType Error + b; // $ExpectType string }); - p.then((a, b) => { - a; // $ExpectType string - b; // $ExpectType JQuery - }, null); - p.then((a, b) => { a; // $ExpectType string b; // $ExpectType JQuery }, null, null); - p.then(null); + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }, null); - p.then(null, null); - - p.then(null, null, null); + p.then((a, b) => { + a; // $ExpectType string + b; // $ExpectType JQuery + }); function doneFilter() { p.then(() => { From c735b73233baab5e9ff3b0e30ca5c644c9aec0b4 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 18:16:04 -0400 Subject: [PATCH 27/35] [jquery] Fix Promise and Deferred. Fix when() ordering. --- types/jquery/index.d.ts | 851 +++++++++++++---------------------- types/jquery/jquery-tests.ts | 39 +- 2 files changed, 319 insertions(+), 571 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 862cd57e4a..658a463c83 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3267,20 +3267,6 @@ interface JQueryStatic { * @since 1.12-2.2 */ uniqueSort(array: T[]): T[]; - /** - * Provides a way to execute callback functions based on zero or more Thenable objects, usually - * Deferred objects that represent asynchronous events. - * - * @see {@link https://api.jquery.com/jQuery.when/} - * @since 1.5 - */ - when - (deferredT: JQuery.Promise | JQuery.Thenable | TR1, - deferredU: JQuery.Promise | JQuery.Thenable | UR1, - deferredV: JQuery.Promise | JQuery.Thenable | VR1): JQuery.Promise3; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3312,11 +3298,13 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when + when (deferredT: JQuery.Promise | JQuery.Thenable | TR1, - deferredU: JQuery.Promise | JQuery.Thenable | UR1): JQuery.Promise2; + deferredU: JQuery.Promise | JQuery.Thenable | UR1, + deferredV: JQuery.Promise | JQuery.Thenable | VR1): JQuery.Promise3; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3342,7 +3330,11 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when(deferred: JQuery.Promise | JQuery.Thenable | TR1): JQuery.Promise; + when + (deferredT: JQuery.Promise | JQuery.Thenable | TR1, + deferredU: JQuery.Promise | JQuery.Thenable | UR1): JQuery.Promise2; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3355,6 +3347,14 @@ interface JQueryStatic { TR3 = never, TJ3 = never> (deferredT: JQuery.Promise3 | JQuery.Promise2): JQuery.Promise3; + /** + * Provides a way to execute callback functions based on zero or more Thenable objects, usually + * Deferred objects that represent asynchronous events. + * + * @see {@link https://api.jquery.com/jQuery.when/} + * @since 1.5 + */ + when(deferred: JQuery.Promise | JQuery.Thenable | TR1): JQuery.Promise; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -4271,351 +4271,6 @@ declare namespace JQuery { */ interface Thenable extends PromiseLike { } - interface DeferredStatic { - // https://jquery.com/upgrade-guide/3.0/#callback-exit - exceptionHook: any; - (beforeStart?: (this: JQuery.Deferred, deferred: JQuery.Deferred) => void): JQuery.Deferred; - } - - interface Deferred { - /** - * Add handlers to be called when the Deferred object is either resolved or rejected. - * - * @param alwaysCallback A function, or array of functions, that is called when the Deferred is resolved or rejected. - * @param alwaysCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved or rejected. - * @see {@link https://api.jquery.com/deferred.always/} - * @since 1.6 - */ - always(alwaysCallback: TypeOrArray>, - ...alwaysCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is resolved. - * - * @param doneCallback A function, or array of functions, that are called when the Deferred is resolved. - * @param doneCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.done/} - * @since 1.5 - */ - done(doneCallback: TypeOrArray>, - ...doneCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failCallback A function, or array of functions, that are called when the Deferred is rejected. - * @param failCallbacks Optional additional functions, or arrays of functions, that are called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.fail/} - * @since 1.5 - */ - fail(failCallback: TypeOrArray>, - ...failCallbacks: Array>>): this; - /** - * Add handlers to be called when the Deferred object generates progress notifications. - * - * @param progressCallback A function, or array of functions, to be called when the Deferred generates progress notifications. - * @param progressCallbacks Optional additional functions, or arrays of functions, to be called when the Deferred generates - * progress notifications. - * @see {@link https://api.jquery.com/deferred.progress/} - * @since 1.7 - */ - progress(progressCallback: TypeOrArray>, - ...progressCallbacks: Array>>): this; - /** - * Return a Deferred's Promise object. - * - * @param target Object onto which the promise methods have to be attached - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(target: TTarget): JQuery.Promise & TTarget; - /** - * Return a Deferred's Promise object. - * - * @see {@link https://api.jquery.com/deferred.promise/} - * @since 1.5 - */ - promise(): JQuery.Promise; - /** - * Determine the current state of a Deferred object. - * - * @see {@link https://api.jquery.com/deferred.state/} - * @since 1.7 - */ - state(): 'pending' | 'resolved' | 'rejected'; - - /** - * Add handlers to be called when the Deferred object is rejected. - * - * @param failFilter A function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.catch/} - * @since 3.0 - */ - catch(failFilter: (...reasons: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; - - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; - - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; - - /** - * Call the progressCallbacks on a Deferred object with the given args. - * - * @param args Optional arguments that are passed to the progressCallbacks. - * @see {@link https://api.jquery.com/deferred.notify/} - * @since 1.7 - */ - notify(...args: TN[]): this; - /** - * Call the progressCallbacks on a Deferred object with the given context and args. - * - * @param context Context passed to the progressCallbacks as the this object. - * @param args An optional array of arguments that are passed to the progressCallbacks. - * @see {@link https://api.jquery.com/deferred.notifyWith/} - * @since 1.7 - */ - notifyWith(context: object, args?: ArrayLike): this; - /** - * Reject a Deferred object and call any failCallbacks with the given args. - * - * @param args Optional arguments that are passed to the failCallbacks. - * @see {@link https://api.jquery.com/deferred.reject/} - * @since 1.5 - */ - reject(...args: TJ[]): this; - /** - * Reject a Deferred object and call any failCallbacks with the given context and args. - * - * @param context Context passed to the failCallbacks as the this object. - * @param args An optional array of arguments that are passed to the failCallbacks. - * @see {@link https://api.jquery.com/deferred.rejectWith/} - * @since 1.5 - */ - rejectWith(context: object, args?: ArrayLike): this; - /** - * Resolve a Deferred object and call any doneCallbacks with the given args. - * - * @param args Optional arguments that are passed to the doneCallbacks. - * @see {@link https://api.jquery.com/deferred.resolve/} - * @since 1.5 - */ - resolve(...args: TR[]): this; - /** - * Resolve a Deferred object and call any doneCallbacks with the given context and args. - * - * @param context Context passed to the doneCallbacks as the this object. - * @param args An optional array of arguments that are passed to the doneCallbacks. - * @see {@link https://api.jquery.com/deferred.resolveWith/} - * @since 1.5 - */ - resolveWith(context: object, args?: ArrayLike): this; - } - - namespace Deferred { - interface Callback3 { - (t: T, u: U, v: V): void; - } - - interface Callback { - (...args: T[]): void; - } - - /** - * @deprecated - */ - interface DoneCallback extends Callback { } - - /** - * @deprecated - */ - interface FailCallback extends Callback { } - - /** - * @deprecated - */ - interface AlwaysCallback extends Callback { } - - /** - * @deprecated - */ - interface ProgressCallback extends Callback { } - } - // Type parameter guide // -------------------- // Each type parameter represents a parameter in one of the three possible callbacks. @@ -5114,14 +4769,14 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.promise/} * @since 1.5 */ - promise(target: TTarget): JQuery.Promise & TTarget; + promise(target: TTarget): JQuery.Promise & TTarget; /** * Return a Deferred's Promise object. * * @see {@link https://api.jquery.com/deferred.promise/} * @since 1.5 */ - promise(): JQuery.Promise; + promise(): JQuery.Promise; /** * Determine the current state of a Deferred object. * @@ -5130,6 +4785,207 @@ declare namespace JQuery { */ state(): 'pending' | 'resolved' | 'rejected'; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter: (...t: TR[]) => Promise | Thenable | AJF, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TR[]) => Promise | Thenable | AJF, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter: null, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TR[]) => Promise | Thenable | AJF, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter: (...t: TJ[]) => Promise | Thenable | AJF, + progressFilter?: null): Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: null, + failFilter: (...t: TJ[]) => Promise | Thenable | AJF, + progressFilter?: null): Promise; + /** + * Utility method to filter and/or chain Deferreds. + * + * @param doneFilter An optional function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.pipe/} + * @since 1.6 + * @since 1.7 + * @deprecated 1.8 + */ + pipe + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter?: null, + progressFilter?: null): Promise; + + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter: (...t: TR[]) => Promise | Thenable | ARF, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TR[]) => Promise | Thenable | ARF, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter: null, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: null, + progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter: (...t: TJ[]) => Promise | Thenable | ARF, + progressFilter?: null): Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @param failFilter An optional function that is called when the Deferred is rejected. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TJ[]) => Promise | Thenable | ARF, + progressFilter?: null): Promise; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @param doneFilter A function that is called when the Deferred is resolved. + * @see {@link https://api.jquery.com/deferred.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, + failFilter?: null, + progressFilter?: null): Promise; + /** * Add handlers to be called when the Deferred object is rejected. * @@ -5137,197 +4993,98 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.catch/} * @since 3.0 */ - catch(failFilter: (...reasons: TJ[]) => JQuery.Promise | Thenable | AR): JQuery.Promise; + catch + (failFilter: (...t: TJ[]) => Promise | Thenable | ARF): Promise; + } + + interface DeferredStatic { + // https://jquery.com/upgrade-guide/3.0/#callback-exit + exceptionHook: any; + (beforeStart?: (this: JQuery.Deferred, deferred: JQuery.Deferred) => void): JQuery.Deferred; + } + + interface Deferred extends JQuery.Promise { + /** + * Call the progressCallbacks on a Deferred object with the given args. + * + * @param args Optional arguments that are passed to the progressCallbacks. + * @see {@link https://api.jquery.com/deferred.notify/} + * @since 1.7 + */ + notify(...args: TN[]): this; + /** + * Call the progressCallbacks on a Deferred object with the given context and args. + * + * @param context Context passed to the progressCallbacks as the this object. + * @param args An optional array of arguments that are passed to the progressCallbacks. + * @see {@link https://api.jquery.com/deferred.notifyWith/} + * @since 1.7 + */ + notifyWith(context: object, args?: ArrayLike): this; + /** + * Reject a Deferred object and call any failCallbacks with the given args. + * + * @param args Optional arguments that are passed to the failCallbacks. + * @see {@link https://api.jquery.com/deferred.reject/} + * @since 1.5 + */ + reject(...args: TJ[]): this; + /** + * Reject a Deferred object and call any failCallbacks with the given context and args. + * + * @param context Context passed to the failCallbacks as the this object. + * @param args An optional array of arguments that are passed to the failCallbacks. + * @see {@link https://api.jquery.com/deferred.rejectWith/} + * @since 1.5 + */ + rejectWith(context: object, args?: ArrayLike): this; + /** + * Resolve a Deferred object and call any doneCallbacks with the given args. + * + * @param args Optional arguments that are passed to the doneCallbacks. + * @see {@link https://api.jquery.com/deferred.resolve/} + * @since 1.5 + */ + resolve(...args: TR[]): this; + /** + * Resolve a Deferred object and call any doneCallbacks with the given context and args. + * + * @param context Context passed to the doneCallbacks as the this object. + * @param args An optional array of arguments that are passed to the doneCallbacks. + * @see {@link https://api.jquery.com/deferred.resolveWith/} + * @since 1.5 + */ + resolveWith(context: object, args?: ArrayLike): this; + } + + namespace Deferred { + interface Callback3 { + (t: T, u: U, v: V): void; + } + + interface Callback { + (...args: T[]): void; + } /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 + * @deprecated */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, - progressFilter?: null): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR, - progressFilter?: null): JQuery.Promise; - /** - * Utility method to filter and/or chain Deferreds. - * - * @param doneFilter An optional function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.pipe/} - * @since 1.6 - * @since 1.7 - * @deprecated 1.8 - */ - pipe - (doneFilter: ((...t: TR[]) => JQuery.Promise | Thenable | AR) | null, - failFilter?: null, - progressFilter?: null): JQuery.Promise; + interface DoneCallback extends Callback { } /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 + * @deprecated */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN3): JQuery.Promise; + interface FailCallback extends Callback { } + /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 + * @deprecated */ - then - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR1, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; + interface AlwaysCallback extends Callback { } + /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 + * @deprecated */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN1): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @param progressFilter An optional function that is called when progress notifications are sent to the Deferred. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: null, - progressFilter: (...t: TN[]) => JQuery.Promise | Thenable | AN): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: (...t: TR[]) => JQuery.Promise | Thenable | AR1, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR2, - progressFilter?: null): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @param failFilter An optional function that is called when the Deferred is rejected. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: null, - failFilter: (...t: TJ[]) => JQuery.Promise | Thenable | AR, - progressFilter?: null): JQuery.Promise; - /** - * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. - * - * @param doneFilter A function that is called when the Deferred is resolved. - * @see {@link https://api.jquery.com/deferred.then/} - * @since 1.8 - */ - then - (doneFilter: ((...t: TR[]) => JQuery.Promise | Thenable | AR) | null, - failFilter?: null, - progressFilter?: null): JQuery.Promise; + interface ProgressCallback extends Callback { } } // endregion diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 7347a8a415..e6a9907958 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4579,9 +4579,9 @@ function JQueryStatic() { } function Promise2() { - const t = $.Deferred() as JQuery.Promise2; - const u = $.Deferred() as JQuery.Promise2; - const v = $.Deferred() as JQuery.Promise2; + const t: JQuery.Promise2 = {} as any; + const u: JQuery.Promise2 = {} as any; + const v: JQuery.Promise2 = {} as any; // 3 parameters { @@ -4746,12 +4746,9 @@ function JQueryStatic() { $.when(task1, task2, task3) .done((r1, r2, r3) => { - // $ExpectType One - r1; - // $ExpectType Two - r2; - // $ExpectType Three - r3; + r1; // $ExpectType One + r2; // $ExpectType Two + r3; // $ExpectType Three }); } @@ -5916,39 +5913,33 @@ function _Promise(p: JQuery.Promise) { a; // $ExpectType string }, (a) => { a; // $ExpectType Error - }); + }, null); p.then((a) => { a; // $ExpectType string }, (a) => { a; // $ExpectType Error - }, null); - - p.then(null, (a) => { - a; // $ExpectType Error }); p.then(null, (a) => { a; // $ExpectType Error }, null); - p.then((a) => { - a; // $ExpectType string + p.then(null, (a) => { + a; // $ExpectType Error }); - p.then((a) => { - a; // $ExpectType string - }, null); - p.then((a) => { a; // $ExpectType string }, null, null); - p.then(null); + p.then((a) => { + a; // $ExpectType string + }, null); - p.then(null, null); - - p.then(null, null, null); + p.then((a) => { + a; // $ExpectType string + }); function doneFilter() { p.then(() => { From b40c38c085b086026b70127f4d012d9e6dfe6fa4 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 18:34:56 -0400 Subject: [PATCH 28/35] [jquery] Fix Promise. --- types/jquery/index.d.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 658a463c83..ab44375843 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4714,6 +4714,16 @@ declare namespace JQuery { UR, UJ, UN, never, never, never> { } + /** + * This object provides a subset of the methods of the Deferred object (then, done, fail, always, + * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. + * + * @see {@link http://api.jquery.com/Types/#Promise} + */ + interface Promise1 extends Promise3 { } + /** * This object provides a subset of the methods of the Deferred object (then, done, fail, always, * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. @@ -4800,7 +4810,7 @@ declare namespace JQuery { ARF = never, AJF = never, ANF = never, ARP = never, AJP = never, ANP = never> (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, - failFilter: (...t: TR[]) => Promise | Thenable | AJF, + failFilter: (...t: TJ[]) => Promise | Thenable | AJF, progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; /** * Utility method to filter and/or chain Deferreds. @@ -4816,7 +4826,7 @@ declare namespace JQuery { pipe (doneFilter: null, - failFilter: (...t: TR[]) => Promise | Thenable | AJF, + failFilter: (...t: TJ[]) => Promise | Thenable | AJF, progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; /** * Utility method to filter and/or chain Deferreds. @@ -4906,7 +4916,7 @@ declare namespace JQuery { ARF = never, AJF = never, ANF = never, ARP = never, AJP = never, ANP = never> (doneFilter: (...t: TR[]) => Promise | Thenable | ARD, - failFilter: (...t: TR[]) => Promise | Thenable | ARF, + failFilter: (...t: TJ[]) => Promise | Thenable | ARF, progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. @@ -4920,7 +4930,7 @@ declare namespace JQuery { then (doneFilter: null, - failFilter: (...t: TR[]) => Promise | Thenable | ARF, + failFilter: (...t: TJ[]) => Promise | Thenable | ARF, progressFilter: (...t: TN[]) => Promise | Thenable | ANP): Promise; /** * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. From db5a6f4bc4da9c36b89ad8059df0800eaae515cc Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 27 Jun 2017 19:08:27 -0400 Subject: [PATCH 29/35] [jquery] Formatting. --- types/jquery/jquery-tests.ts | 123 ++++++++++++----------------------- 1 file changed, 41 insertions(+), 82 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index e6a9907958..b21aeb6427 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4458,52 +4458,31 @@ function JQueryStatic() { const w = $.when(t, u, v); w.then((a, b, c) => { - // $ExpectType string - a[0]; - // $ExpectType SuccessTextStatus - a[1]; - // $ExpectType jqXHR - a[2]; - // $ExpectType number - b[0]; - // $ExpectType SuccessTextStatus - b[1]; - // $ExpectType jqXHR - b[2]; - // $ExpectType boolean - c[0]; - // $ExpectType SuccessTextStatus - c[1]; - // $ExpectType jqXHR - c[2]; + a[0]; // $ExpectType string + a[1]; // $ExpectType SuccessTextStatus + a[2]; // $ExpectType jqXHR + b[0]; // $ExpectType number + b[1]; // $ExpectType SuccessTextStatus + b[2]; // $ExpectType jqXHR + c[0]; // $ExpectType boolean + c[1]; // $ExpectType SuccessTextStatus + c[2]; // $ExpectType jqXHR }); w.catch((a, b, c) => { - // $ExpectType jqXHR - a[0]; - // $ExpectType ErrorTextStatus - a[1]; - // $ExpectType string - a[2]; - // $ExpectType jqXHR - b[0]; - // $ExpectType ErrorTextStatus - b[1]; - // $ExpectType string - b[2]; - // $ExpectType jqXHR - c[0]; - // $ExpectType ErrorTextStatus - c[1]; - // $ExpectType string - c[2]; + a[0]; // $ExpectType jqXHR + a[1]; // $ExpectType ErrorTextStatus + a[2]; // $ExpectType string + b[0]; // $ExpectType jqXHR + b[1]; // $ExpectType ErrorTextStatus + b[2]; // $ExpectType string + c[0]; // $ExpectType jqXHR + c[1]; // $ExpectType ErrorTextStatus + c[2]; // $ExpectType string }); w.then(null, null, (a, b, c) => { - // $ExpectType never - a; - // $ExpectType never - b; - // $ExpectType never - c; + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never }); } @@ -4512,38 +4491,24 @@ function JQueryStatic() { const w = $.when(t, u); w.then((a, b) => { - // $ExpectType string - a[0]; - // $ExpectType SuccessTextStatus - a[1]; - // $ExpectType jqXHR - a[2]; - // $ExpectType number - b[0]; - // $ExpectType SuccessTextStatus - b[1]; - // $ExpectType jqXHR - b[2]; + a[0]; // $ExpectType string + a[1]; // $ExpectType SuccessTextStatus + a[2]; // $ExpectType jqXHR + b[0]; // $ExpectType number + b[1]; // $ExpectType SuccessTextStatus + b[2]; // $ExpectType jqXHR }); w.catch((a, b) => { - // $ExpectType jqXHR - a[0]; - // $ExpectType ErrorTextStatus - a[1]; - // $ExpectType string - a[2]; - // $ExpectType jqXHR - b[0]; - // $ExpectType ErrorTextStatus - b[1]; - // $ExpectType string - b[2]; + a[0]; // $ExpectType jqXHR + a[1]; // $ExpectType ErrorTextStatus + a[2]; // $ExpectType string + b[0]; // $ExpectType jqXHR + b[1]; // $ExpectType ErrorTextStatus + b[2]; // $ExpectType string }); w.then(null, null, (a, b) => { - // $ExpectType never - a; - // $ExpectType never - b; + a; // $ExpectType never + b; // $ExpectType never }); } @@ -4568,12 +4533,9 @@ function JQueryStatic() { errorThrown; }); w.then(null, null, (a, b, c) => { - // $ExpectType never - a; - // $ExpectType never - b; - // $ExpectType never - c; + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never }); } } @@ -4732,16 +4694,13 @@ function JQueryStatic() { const task3 = this.runTask3(); task1.then(value => { - // $ExpectType One - value; + value; // $ExpectType One }); task2.then(value => { - // $ExpectType Two - value; + value; // $ExpectType Two }); task3.then(value => { - // $ExpectType Three - value; + value; // $ExpectType Three }); $.when(task1, task2, task3) From 9dee232fbed76aaa6ec37f82d9cc04fe8db6b49d Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Wed, 28 Jun 2017 08:16:31 -0400 Subject: [PATCH 30/35] [jquery] Formatting. --- types/jquery/jquery-tests.ts | 102 ++++++++++++----------------------- 1 file changed, 35 insertions(+), 67 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index b21aeb6427..efb87af785 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4467,8 +4467,7 @@ function JQueryStatic() { c[0]; // $ExpectType boolean c[1]; // $ExpectType SuccessTextStatus c[2]; // $ExpectType jqXHR - }); - w.catch((a, b, c) => { + }, (a, b, c) => { a[0]; // $ExpectType jqXHR a[1]; // $ExpectType ErrorTextStatus a[2]; // $ExpectType string @@ -4478,8 +4477,7 @@ function JQueryStatic() { c[0]; // $ExpectType jqXHR c[1]; // $ExpectType ErrorTextStatus c[2]; // $ExpectType string - }); - w.then(null, null, (a, b, c) => { + }, (a, b, c) => { a; // $ExpectType never b; // $ExpectType never c; // $ExpectType never @@ -4497,16 +4495,14 @@ function JQueryStatic() { b[0]; // $ExpectType number b[1]; // $ExpectType SuccessTextStatus b[2]; // $ExpectType jqXHR - }); - w.catch((a, b) => { + }, (a, b) => { a[0]; // $ExpectType jqXHR a[1]; // $ExpectType ErrorTextStatus a[2]; // $ExpectType string b[0]; // $ExpectType jqXHR b[1]; // $ExpectType ErrorTextStatus b[2]; // $ExpectType string - }); - w.then(null, null, (a, b) => { + }, (a, b) => { a; // $ExpectType never b; // $ExpectType never }); @@ -4516,23 +4512,15 @@ function JQueryStatic() { { const w = $.when(t); - w.then((data, textStatus, jqXHR) => { - // $ExpectType string - data; - // $ExpectType SuccessTextStatus - textStatus; - // $ExpectType jqXHR - jqXHR; - }); - w.catch((jqXHR, textStatus, errorThrown) => { - // $ExpectType jqXHR - jqXHR; - // $ExpectType ErrorTextStatus - textStatus; - // $ExpectType string - errorThrown; - }); - w.then(null, null, (a, b, c) => { + w.then((a, b, c) => { + a; // $ExpectType string + b; // $ExpectType SuccessTextStatus + c; // $ExpectType jqXHR + }, (a, b, c) => { + a; // $ExpectType jqXHR + b; // $ExpectType ErrorTextStatus + c; // $ExpectType string + }, (a, b, c) => { a; // $ExpectType never b; // $ExpectType never c; // $ExpectType never @@ -4556,16 +4544,14 @@ function JQueryStatic() { b[1]; // $ExpectType boolean c[0]; // $ExpectType string c[1]; // $ExpectType boolean - }); - w.catch((a, b, c) => { + }, (a, b, c) => { a[0]; // $ExpectType Error a[1]; // $ExpectType any b[0]; // $ExpectType Error b[1]; // $ExpectType any c[0]; // $ExpectType Error c[1]; // $ExpectType any - }); - w.then(null, null, (a, b, c) => { + }, (a, b, c) => { a; // $ExpectType never b; // $ExpectType never c; // $ExpectType never @@ -4581,14 +4567,12 @@ function JQueryStatic() { a[1]; // $ExpectType boolean b[0]; // $ExpectType string b[1]; // $ExpectType boolean - }); - w.catch((a, b) => { + }, (a, b) => { a[0]; // $ExpectType Error a[1]; // $ExpectType any b[0]; // $ExpectType Error b[1]; // $ExpectType any - }); - w.then(null, null, (a, b) => { + }, (a, b) => { a; // $ExpectType never b; // $ExpectType never }); @@ -4601,12 +4585,10 @@ function JQueryStatic() { w.then((a, b) => { a; // $ExpectType string b; // $ExpectType boolean - }); - w.catch((a, b) => { + }, (a, b) => { a; // $ExpectType Error b; // $ExpectType any - }); - w.then(null, null, (a, b) => { + }, (a, b) => { a; // $ExpectType never b; // $ExpectType never }); @@ -4616,54 +4598,40 @@ function JQueryStatic() { function Promise() { const w = $.when($.Deferred()); - w.then(value => { - // $ExpectType string - value; - }); - w.catch(reason => { - // $ExpectType Error - reason; - }); - w.then(null, null, value => { - // $ExpectType never - value; + w.then(a => { + a; // $ExpectType string + }, a => { + a; // $ExpectType Error + }, a => { + a; // $ExpectType never }); } function Thenable() { const w = $.when($.Deferred()); - w.then(value => { - // $ExpectType string - value; + w.then(a => { + a; // $ExpectType string }); } function value() { const w = $.when('myVal1'); - w.then(value => { - // $ExpectType string - value; + w.then(a => { + a; // $ExpectType string }); } function Zero() { const w = $.when(); - w.then((value) => { - // $ExpectType never - value; - }); - - w.catch((reason) => { - // $ExpectType never - reason; - }); - - w.then(null, null, (value) => { - // $ExpectType never - value; + w.then(a => { + a; // $ExpectType never + }, a => { + a; // $ExpectType never + }, a => { + a; // $ExpectType never }); } From 306806e1213c618c9796cadba789632f23202d62 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Wed, 28 Jun 2017 09:01:38 -0400 Subject: [PATCH 31/35] [jquery] More tests. --- types/jquery/jquery-tests.ts | 140 ++++++++++++++++++++++++++++------- 1 file changed, 114 insertions(+), 26 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index efb87af785..3e93fc84b8 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4448,6 +4448,16 @@ function JQueryStatic() { } function when() { + interface I1 { kind: 'I1'; } + interface I2 { kind: 'I2'; } + interface I3 { kind: 'I3'; } + interface I4 { kind: 'I4'; } + interface I5 { kind: 'I5'; } + interface I6 { kind: 'I6'; } + interface I7 { kind: 'I7'; } + interface I8 { kind: 'I8'; } + interface I9 { kind: 'I9'; } + function Promise3() { const t = $.ajax() as JQuery.jqXHR; const u = $.ajax() as JQuery.jqXHR; @@ -4596,23 +4606,111 @@ function JQueryStatic() { } function Promise() { - const w = $.when($.Deferred()); + const p1: JQuery.Promise = {} as any; + const p2: JQuery.Promise = {} as any; + const p3: JQuery.Promise = {} as any; - w.then(a => { - a; // $ExpectType string - }, a => { - a; // $ExpectType Error - }, a => { - a; // $ExpectType never - }); + // 3 parameters + { + const w = $.when(p1, p2, p3); + + w.then((a, b, c) => { + a; // $ExpectType I1 + b; // $ExpectType I2 + c; // $ExpectType I3 + }, (a, b, c) => { + a; // $ExpectType I2 + b; // $ExpectType I3 + c; // $ExpectType I4 + }, (a, b, c) => { + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never + }); + } + + // 2 parameters + { + const w = $.when(p1, p2); + + w.then((a, b) => { + a; // $ExpectType I1 + b; // $ExpectType I2 + }, (a, b) => { + a; // $ExpectType I2 + b; // $ExpectType I3 + }, (a, b) => { + a; // $ExpectType never + b; // $ExpectType never + }); + } + + // 1 parameter + { + const w = $.when(p1); + + w.then(a => { + a; // $ExpectType I1 + }, a => { + a; // $ExpectType I2 + }, a => { + a; // $ExpectType never + }); + } } function Thenable() { - const w = $.when($.Deferred()); + const t1: JQuery.Thenable = {} as any; + const t2: JQuery.Thenable = {} as any; + const t3: JQuery.Thenable = {} as any; - w.then(a => { - a; // $ExpectType string - }); + // 3 parameters + { + const w = $.when(t1, t2, t3); + + w.then((a, b, c) => { + a; // $ExpectType I1 + b; // $ExpectType I2 + c; // $ExpectType I3 + }, (a, b, c) => { + a; // $ExpectType any + b; // $ExpectType any + c; // $ExpectType any + }, (a, b, c) => { + a; // $ExpectType never + b; // $ExpectType never + c; // $ExpectType never + }); + } + + // 2 parameters + { + const w = $.when(t1, t2); + + w.then((a, b) => { + a; // $ExpectType I1 + b; // $ExpectType I2 + }, (a, b) => { + a; // $ExpectType any + b; // $ExpectType any + }, (a, b) => { + a; // $ExpectType never + b; // $ExpectType never + }); + } + + // 1 parameter + { + const w = $.when(t1); + + w.then(a => { + a; // $ExpectType I1 + }, a => { + a; // $ExpectType any + }, a => { + a; // $ExpectType never + }); + } } function value() { @@ -4657,22 +4755,12 @@ function JQueryStatic() { class AsyncRunner { Run(): void { - const task1 = this.runTask1(); - const task2 = this.runTask2(); - const task3 = this.runTask3(); - - task1.then(value => { - value; // $ExpectType One - }); - task2.then(value => { - value; // $ExpectType Two - }); - task3.then(value => { - value; // $ExpectType Three - }); + const task1: JQuery.Promise = {} as any; + const task2: JQuery.Promise = this.runTask2(); + const task3: JQuery.Promise = this.runTask3(); $.when(task1, task2, task3) - .done((r1, r2, r3) => { + .then((r1, r2, r3) => { r1; // $ExpectType One r2; // $ExpectType Two r3; // $ExpectType Three From a6134ee5a1e6db498f570e98bb77aeaf8fa02cbc Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Wed, 28 Jun 2017 13:28:42 -0400 Subject: [PATCH 32/35] [jquery] Remove support for multiple multi-argument Promises from when(). --- types/jquery/index.d.ts | 42 ------- types/jquery/jquery-tests.ts | 205 +++++++++++++++++------------------ 2 files changed, 102 insertions(+), 145 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index ab44375843..834ee4c70d 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -3267,30 +3267,6 @@ interface JQueryStatic { * @since 1.12-2.2 */ uniqueSort(array: T[]): T[]; - /** - * Provides a way to execute callback functions based on zero or more Thenable objects, usually - * Deferred objects that represent asynchronous events. - * - * @see {@link https://api.jquery.com/jQuery.when/} - * @since 1.5 - */ - when - (deferredT: JQuery.Promise3 | - JQuery.Promise2, - deferredU: JQuery.Promise3 | - JQuery.Promise2, - deferredV: JQuery.Promise3 | - JQuery.Promise2): JQuery.Promise3<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, - [UR1, UR2, UR3], [UJ1, UJ2, UJ3], never, - [VR1, VR2, VR3], [VJ1, VJ2, VJ3], never>; /** * Provides a way to execute callback functions based on zero or more Thenable objects, usually * Deferred objects that represent asynchronous events. @@ -3312,24 +3288,6 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.when/} * @since 1.5 */ - when - (deferredT: JQuery.Promise3 | - JQuery.Promise2, - deferredU: JQuery.Promise3 | - JQuery.Promise2): JQuery.Promise2<[TR1, TR2, TR3], [TJ1, TJ2, TJ3], never, - [UR1, UR2, UR3], [UJ1, UJ2, UJ3], never>; - /** - * Provides a way to execute callback functions based on zero or more Thenable objects, usually - * Deferred objects that represent asynchronous events. - * - * @see {@link https://api.jquery.com/jQuery.when/} - * @since 1.5 - */ when (deferredT: JQuery.Promise | JQuery.Thenable | TR1, diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 3e93fc84b8..325a288889 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -4463,60 +4463,60 @@ function JQueryStatic() { const u = $.ajax() as JQuery.jqXHR; const v = $.ajax() as JQuery.jqXHR; - // 3 parameters - { - const w = $.when(t, u, v); - - w.then((a, b, c) => { - a[0]; // $ExpectType string - a[1]; // $ExpectType SuccessTextStatus - a[2]; // $ExpectType jqXHR - b[0]; // $ExpectType number - b[1]; // $ExpectType SuccessTextStatus - b[2]; // $ExpectType jqXHR - c[0]; // $ExpectType boolean - c[1]; // $ExpectType SuccessTextStatus - c[2]; // $ExpectType jqXHR - }, (a, b, c) => { - a[0]; // $ExpectType jqXHR - a[1]; // $ExpectType ErrorTextStatus - a[2]; // $ExpectType string - b[0]; // $ExpectType jqXHR - b[1]; // $ExpectType ErrorTextStatus - b[2]; // $ExpectType string - c[0]; // $ExpectType jqXHR - c[1]; // $ExpectType ErrorTextStatus - c[2]; // $ExpectType string - }, (a, b, c) => { - a; // $ExpectType never - b; // $ExpectType never - c; // $ExpectType never - }); - } - - // 2 parameters - { - const w = $.when(t, u); - - w.then((a, b) => { - a[0]; // $ExpectType string - a[1]; // $ExpectType SuccessTextStatus - a[2]; // $ExpectType jqXHR - b[0]; // $ExpectType number - b[1]; // $ExpectType SuccessTextStatus - b[2]; // $ExpectType jqXHR - }, (a, b) => { - a[0]; // $ExpectType jqXHR - a[1]; // $ExpectType ErrorTextStatus - a[2]; // $ExpectType string - b[0]; // $ExpectType jqXHR - b[1]; // $ExpectType ErrorTextStatus - b[2]; // $ExpectType string - }, (a, b) => { - a; // $ExpectType never - b; // $ExpectType never - }); - } + // // 3 parameters + // { + // const w = $.when(t, u, v); + // + // w.then((a, b, c) => { + // a[0]; // $ExpectType string + // a[1]; // $ExpectType SuccessTextStatus + // a[2]; // $ExpectType jqXHR + // b[0]; // $ExpectType number + // b[1]; // $ExpectType SuccessTextStatus + // b[2]; // $ExpectType jqXHR + // c[0]; // $ExpectType boolean + // c[1]; // $ExpectType SuccessTextStatus + // c[2]; // $ExpectType jqXHR + // }, (a, b, c) => { + // a[0]; // $ExpectType jqXHR + // a[1]; // $ExpectType ErrorTextStatus + // a[2]; // $ExpectType string + // b[0]; // $ExpectType jqXHR + // b[1]; // $ExpectType ErrorTextStatus + // b[2]; // $ExpectType string + // c[0]; // $ExpectType jqXHR + // c[1]; // $ExpectType ErrorTextStatus + // c[2]; // $ExpectType string + // }, (a, b, c) => { + // a; // $ExpectType never + // b; // $ExpectType never + // c; // $ExpectType never + // }); + // } + // + // // 2 parameters + // { + // const w = $.when(t, u); + // + // w.then((a, b) => { + // a[0]; // $ExpectType string + // a[1]; // $ExpectType SuccessTextStatus + // a[2]; // $ExpectType jqXHR + // b[0]; // $ExpectType number + // b[1]; // $ExpectType SuccessTextStatus + // b[2]; // $ExpectType jqXHR + // }, (a, b) => { + // a[0]; // $ExpectType jqXHR + // a[1]; // $ExpectType ErrorTextStatus + // a[2]; // $ExpectType string + // b[0]; // $ExpectType jqXHR + // b[1]; // $ExpectType ErrorTextStatus + // b[2]; // $ExpectType string + // }, (a, b) => { + // a; // $ExpectType never + // b; // $ExpectType never + // }); + // } // 1 parameter { @@ -4543,50 +4543,50 @@ function JQueryStatic() { const u: JQuery.Promise2 = {} as any; const v: JQuery.Promise2 = {} as any; - // 3 parameters - { - const w = $.when(t, u, v); - - w.then((a, b, c) => { - a[0]; // $ExpectType string - a[1]; // $ExpectType boolean - b[0]; // $ExpectType string - b[1]; // $ExpectType boolean - c[0]; // $ExpectType string - c[1]; // $ExpectType boolean - }, (a, b, c) => { - a[0]; // $ExpectType Error - a[1]; // $ExpectType any - b[0]; // $ExpectType Error - b[1]; // $ExpectType any - c[0]; // $ExpectType Error - c[1]; // $ExpectType any - }, (a, b, c) => { - a; // $ExpectType never - b; // $ExpectType never - c; // $ExpectType never - }); - } - - // 2 parameters - { - const w = $.when(t, u); - - w.then((a, b) => { - a[0]; // $ExpectType string - a[1]; // $ExpectType boolean - b[0]; // $ExpectType string - b[1]; // $ExpectType boolean - }, (a, b) => { - a[0]; // $ExpectType Error - a[1]; // $ExpectType any - b[0]; // $ExpectType Error - b[1]; // $ExpectType any - }, (a, b) => { - a; // $ExpectType never - b; // $ExpectType never - }); - } + // // 3 parameters + // { + // const w = $.when(t, u, v); + // + // w.then((a, b, c) => { + // a[0]; // $ExpectType string + // a[1]; // $ExpectType boolean + // b[0]; // $ExpectType string + // b[1]; // $ExpectType boolean + // c[0]; // $ExpectType string + // c[1]; // $ExpectType boolean + // }, (a, b, c) => { + // a[0]; // $ExpectType Error + // a[1]; // $ExpectType any + // b[0]; // $ExpectType Error + // b[1]; // $ExpectType any + // c[0]; // $ExpectType Error + // c[1]; // $ExpectType any + // }, (a, b, c) => { + // a; // $ExpectType never + // b; // $ExpectType never + // c; // $ExpectType never + // }); + // } + // + // // 2 parameters + // { + // const w = $.when(t, u); + // + // w.then((a, b) => { + // a[0]; // $ExpectType string + // a[1]; // $ExpectType boolean + // b[0]; // $ExpectType string + // b[1]; // $ExpectType boolean + // }, (a, b) => { + // a[0]; // $ExpectType Error + // a[1]; // $ExpectType any + // b[0]; // $ExpectType Error + // b[1]; // $ExpectType any + // }, (a, b) => { + // a; // $ExpectType never + // b; // $ExpectType never + // }); + // } // 1 parameter { @@ -4742,8 +4742,7 @@ function JQueryStatic() { $.when().then(() => { return first(); }).then((value) => { - // $ExpectType string - value; + value; // $ExpectType string }); } @@ -4755,9 +4754,9 @@ function JQueryStatic() { class AsyncRunner { Run(): void { - const task1: JQuery.Promise = {} as any; - const task2: JQuery.Promise = this.runTask2(); - const task3: JQuery.Promise = this.runTask3(); + const task1 = this.runTask1(); + const task2 = this.runTask2(); + const task3 = this.runTask3(); $.when(task1, task2, task3) .then((r1, r2, r3) => { From e5bce784caa01b271d039ae1aef6b363921aae5a Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 2 Jul 2017 10:28:55 -0400 Subject: [PATCH 33/35] [jquery] Fix uncaught test errors. --- types/jquery/jquery-tests.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 325a288889..35b91fe1d3 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -1191,9 +1191,6 @@ function JQuery() { this; }); - // $ExpectType JQuery - $('p').toggle('linear'); - // $ExpectType JQuery $('p').toggle(true); @@ -5748,15 +5745,9 @@ function Promise3() { return 1; }); - // $ExpectType Promise3 + // $ExpectType Promise3 $.ajax('/echo/json').catch(() => { - const t: JQuery.Thenable = { - then() { - return Promise.resolve('myValue'); - } - }; - - return t; + return t1; }); // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> From 28dd8dcd3f796225bfb1163fec5aefe2728f0c0f Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 2 Jul 2017 10:58:23 -0400 Subject: [PATCH 34/35] [jquery] Collapse JQuery declarations. Base types were declared separately because JQuery was a class at one point. This used class-interface merging to avoid having to declare base type members. --- types/jquery/index.d.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 834ee4c70d..9ff83fad93 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -38,7 +38,7 @@ declare const $: JQueryStatic; // Used by JQuery.Event type _Event = Event; -interface JQuery { +interface JQuery extends Iterable { /** * A string containing the jQuery version number. * @@ -2394,8 +2394,6 @@ interface JQuery { [n: number]: TElement; } -interface JQuery extends Iterable { } - interface JQueryStatic { /** * A factory function that returns a chainable utility object with methods to register multiple From 10f0a52b7fc260ffae6937262e42b6fefec72e11 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Sun, 2 Jul 2017 11:13:40 -0400 Subject: [PATCH 35/35] [jquery] Remove Promise1. Promise1 is not being used. --- types/jquery/index.d.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 9ff83fad93..b71bfbbed3 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -4670,16 +4670,6 @@ declare namespace JQuery { UR, UJ, UN, never, never, never> { } - /** - * This object provides a subset of the methods of the Deferred object (then, done, fail, always, - * pipe, progress, state and promise) to prevent users from changing the state of the Deferred. - * - * @see {@link http://api.jquery.com/Types/#Promise} - */ - interface Promise1 extends Promise3 { } - /** * This object provides a subset of the methods of the Deferred object (then, done, fail, always, * pipe, progress, state and promise) to prevent users from changing the state of the Deferred.