From f13a191e667165ea51c8e8b24cc17818af1179f3 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Wed, 9 Aug 2017 11:32:01 -0400 Subject: [PATCH] [jquery] Improve compatibility between Promise types. Add `pipe(null, null, progressFilter)` overload. --- types/jquery/index.d.ts | 1117 ++++++++++++++++++++++------------ types/jquery/jquery-tests.ts | 110 ++-- 2 files changed, 789 insertions(+), 438 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index b139de1632..82668c2f9b 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -6271,19 +6271,23 @@ declare namespace JQuery { // T = A = 1st position // U = B = 2nd position // V = C = 3rd position + // S = R = rest 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. * * @see {@link http://api.jquery.com/Types/#Promise} + * @deprecated Experimental. Avoid referncing this type directly in your code. */ - interface Promise3 extends _Promise, PromiseLike { + VR, VJ, VN, + SR, SJ, SN> extends _Promise, PromiseLike { /** * Add handlers to be called when the Deferred object is either resolved or rejected. * @@ -6292,8 +6296,8 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.always/} * @since 1.6 */ - always(alwaysCallback: TypeOrArray>, - ...alwaysCallbacks: Array>>): this; + always(alwaysCallback: TypeOrArray>, + ...alwaysCallbacks: Array>>): this; /** * Add handlers to be called when the Deferred object is resolved. * @@ -6302,8 +6306,8 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.done/} * @since 1.5 */ - done(doneCallback: TypeOrArray>, - ...doneCallbacks: Array>>): this; + done(doneCallback: TypeOrArray>, + ...doneCallbacks: Array>>): this; /** * Add handlers to be called when the Deferred object is rejected. * @@ -6312,8 +6316,8 @@ declare namespace JQuery { * @see {@link https://api.jquery.com/deferred.fail/} * @since 1.5 */ - fail(failCallback: TypeOrArray>, - ...failCallbacks: Array>>): this; + fail(failCallback: TypeOrArray>, + ...failCallbacks: Array>>): this; /** * Add handlers to be called when the Deferred object generates progress notifications. * @@ -6323,8 +6327,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. * @@ -6348,6 +6352,8 @@ declare namespace JQuery { */ state(): 'pending' | 'resolved' | 'rejected'; + // region pipe + /** * Utility method to filter and/or chain Deferreds. * @@ -6362,23 +6368,30 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ, ...s: SJ[]) => PromiseBase | Thenable | AJF, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AJF, + progressFilter: (t: TN, u: UN, v: VN, ...s: SN[]) => PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRD | CRF | CRP, CJD | CJF | CJP, CND | CNF | CNP, + RRD | RRF | RRP, RJD | RJF | RJP, RND | RNF | RNP>; /** * Utility method to filter and/or chain Deferreds. * @@ -6393,18 +6406,23 @@ declare namespace JQuery { pipe + CRP = never, CJP = never, CNP = never, + RRP = never, RJP = never, RNP = never> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 PromiseBase | Thenable | AJF, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | AJF, + progressFilter: (t: TN, u: UN, v: VN, ...s: SN[]) => PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRF | CRP, CJF | CJP, CNF | CNP, + RRF | RRP, RJF | RJP, RNF | RNP>; /** * Utility method to filter and/or chain Deferreds. * @@ -6419,18 +6437,47 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, + CRD, CJD, CND, + RRD, RJD, RND> | Thenable | ARD, failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRD | CRP, CJD | CJP, CND | CNP, + RRD | RRP, RJD | RJP, RND | RNP>; + /** + * 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, ...s: SN[]) => PromiseBase | Thenable | ANP): PromiseBase; /** * Utility method to filter and/or chain Deferreds. * @@ -6445,48 +6492,29 @@ declare namespace JQuery { pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ, ...s: SJ[]) => PromiseBase | Thenable | AJF, - progressFilter?: null): Promise3 | Thenable | AJF, + progressFilter?: null): PromiseBase; - /** - * 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; + CRD | CRF, CJD | CJF, CND | CNF, + RRD | RRF, RJD | RJF, RND | RNF>; /** * 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 @@ -6494,18 +6522,23 @@ declare namespace JQuery { */ pipe + CRF = never, CJF = never, CNF = never, + RRF = never, RJF = never, RNF = never> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 PromiseBase | Thenable | AJF, - progressFilter?: null): Promise3 | Thenable | AJF, + progressFilter?: null): PromiseBase; + CRF, CJF, CNF, + RRF, RJF, RNF>; /** * 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 @@ -6513,19 +6546,26 @@ declare namespace JQuery { */ pipe - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, + CRD, CJD, CND, + RRD, RJD, RND> | Thenable | ARD, failFilter?: null, - progressFilter?: null): Promise3; + CRD, CJD, CND, + RRD, RJD, RND>; + + // endregion + + // region then /** * 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 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.then/} @@ -6534,27 +6574,34 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ, ...s: SJ[]) => PromiseBase | Thenable | ARF, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ARF, + progressFilter: (t: TN, u: UN, v: VN, ...s: SN[]) => PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRD | CRF | CRP, CJD | CJF | CJP, CND | CNF | CNP, + RRD | RRF | RRP, RJD | RJF | RJP, RND | RNF | RNP>; /** * 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 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.then/} @@ -6563,22 +6610,27 @@ declare namespace JQuery { then + CRP = never, CJP = never, CNP = never, + RRP = never, RJP = never, RNP = never> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 PromiseBase | Thenable | ARF, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 | Thenable | ARF, + progressFilter: (t: TN, u: UN, v: VN, ...s: SN[]) => PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRF | CRP, CJF | CJP, CNF | CNP, + RRF | RRP, RJF | RJP, RNF | RNP>; /** * 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 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.then/} @@ -6587,22 +6639,27 @@ declare namespace JQuery { then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, + CRD, CJD, CND, + RRD, RJD, RND> | Thenable | ARD, failFilter: null, - progressFilter: (t: TN, u: UN, v: VN) => Promise3 PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRD | CRP, CJD | CJP, CND | CNP, + RRD | RRP, RJD | RJP, RND | RNP>; /** * 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 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.then/} @@ -6610,72 +6667,92 @@ declare namespace JQuery { */ then + CRP = never, CJP = never, CNP = never, + RRP = never, RJP = never, RNP = never> (doneFilter: null, failFilter: null, - progressFilter?: (t: TN, u: UN, v: VN) => Promise3 PromiseBase | Thenable | ANP): Promise3 | Thenable | ANP): PromiseBase; + CRP, CJP, CNP, + RRP, RJP, RNP>; /** * 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 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.then/} * @since 1.8 */ then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 | Thenable | ARD, + failFilter: (t: TJ, u: UJ, v: VJ, ...s: SJ[]) => PromiseBase | Thenable | ARF, - progressFilter?: null): Promise3 | Thenable | ARF, + progressFilter?: null): PromiseBase; + CRD | CRF, CJD | CJF, CND | CNF, + RRD | RRF, RJD | RJF, RND | RNF>; /** * 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 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.then/} * @since 1.8 */ then + CRF = never, CJF = never, CNF = never, + RRF = never, RJF = never, RNF = never> (doneFilter: null, - failFilter: (t: TJ, u: UJ, v: VJ) => Promise3 PromiseBase | Thenable | ARF, - progressFilter?: null): Promise3 | Thenable | ARF, + progressFilter?: null): PromiseBase; + CRF, CJF, CNF, + RRF, RJF, RNF>; /** * 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 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.then/} * @since 1.8 */ then - (doneFilter: (t: TR, u: UR, v: VR) => Promise3 + (doneFilter: (t: TR, u: UR, v: VR, ...s: SR[]) => PromiseBase | Thenable | ARD, + CRD, CJD, CND, + RRD, RJD, RND> | Thenable | ARD, failFilter?: null, - progressFilter?: null): Promise3; + CRD, CJD, CND, + RRD, RJD, RND>; + + // endregion /** * Add handlers to be called when the Deferred object is rejected. @@ -6686,12 +6763,15 @@ declare namespace JQuery { */ catch - (failFilter?: ((t: TJ, u: UJ, v: VJ) => Promise3 + (failFilter?: ((t: TJ, u: UJ, v: VJ, ...s: SJ[]) => PromiseBase | Thenable | ARF) | undefined | null): Promise3 | Thenable | ARF) | undefined | null): PromiseBase; + CRF, CJF, CNF, + RRF, RJF, RNF>; } /** @@ -6700,9 +6780,11 @@ declare namespace JQuery { * * @see {@link http://api.jquery.com/Types/#Promise} */ - interface Promise2 extends Promise3 extends PromiseBase { } /** @@ -6711,282 +6793,22 @@ declare namespace JQuery { * * @see {@link http://api.jquery.com/Types/#Promise} */ - interface Promise extends _Promise, PromiseLike { - /** - * 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'; + interface Promise2 extends PromiseBase { } - /** - * 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: TJ[]) => 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: TJ[]) => 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: 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. - * - * @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[]) => 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. - * - * @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[]) => Promise | Thenable | ARF): Promise; - } + /** + * 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 Promise extends PromiseBase { } interface DeferredStatic { // https://jquery.com/upgrade-guide/3.0/#callback-exit @@ -6994,7 +6816,7 @@ declare namespace JQuery { (beforeStart?: (this: JQuery.Deferred, deferred: JQuery.Deferred) => void): JQuery.Deferred; } - interface Deferred extends JQuery.Promise { + interface Deferred { /** * Call the progressCallbacks on a Deferred object with the given args. * @@ -7046,13 +6868,500 @@ declare namespace JQuery { * @since 1.5 */ resolveWith(context: object, args?: ArrayLike): this; + + /** + * 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'; + + // region pipe + + /** + * 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[]) => PromiseBase | Thenable | ARD, + failFilter: (...t: TJ[]) => PromiseBase | Thenable | AJF, + progressFilter: (...t: TN[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | AJF, + progressFilter: (...t: TN[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ARD, + failFilter: null, + progressFilter: (...t: TN[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ARD, + failFilter: (...t: TJ[]) => PromiseBase | Thenable | AJF, + progressFilter?: null): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | AJF, + progressFilter?: null): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ARD, + failFilter?: null, + progressFilter?: null): PromiseBase; + + // endregion + + // region then + + /** + * 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[]) => PromiseBase | Thenable | ARD, + failFilter: (...t: TJ[]) => PromiseBase | Thenable | ARF, + progressFilter: (...t: TN[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ARF, + progressFilter: (...t: TN[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ARD, + failFilter: null, + progressFilter: (...t: TN[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * 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[]) => PromiseBase | Thenable | ANP): PromiseBase; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @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.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => PromiseBase | Thenable | ARD, + failFilter: (...t: TJ[]) => PromiseBase | Thenable | ARF, + progressFilter?: null): PromiseBase; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @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.then/} + * @since 1.8 + */ + then + (doneFilter: null, + failFilter: (...t: TJ[]) => PromiseBase | Thenable | ARF, + progressFilter?: null): PromiseBase; + /** + * Add handlers to be called when the Deferred object is resolved, rejected, or still in progress. + * + * @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.then/} + * @since 1.8 + */ + then + (doneFilter: (...t: TR[]) => PromiseBase | Thenable | ARD, + failFilter?: null, + progressFilter?: null): PromiseBase; + + // endregion + + /** + * 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[]) => PromiseBase | Thenable | ARF) | undefined | null): PromiseBase; } namespace Deferred { - interface Callback3 { - (t: T, u: U, v: V): void; + interface CallbackBase { + (t: T, u: U, v: V, ...r: R[]): void; } + interface Callback3 extends CallbackBase { } + interface Callback { (...args: T[]): void; } diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 81ab91d6a3..cbc97b9cd9 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -6179,7 +6179,7 @@ function JQuery() { } } -function AjaxSettings() { +function JQuery_AjaxSettings() { $.ajax({ accepts: { mycustomtype: 'application/x-some-custom-type' @@ -6345,7 +6345,7 @@ function AjaxSettings() { }); } -function Callbacks() { +function JQuery_Callbacks() { function add() { const callbacks = $.Callbacks(); @@ -6425,7 +6425,7 @@ function Callbacks() { } } -function EffectsOptions() { +function JQuery_EffectsOptions() { $('p').show({ always(animation, jumpToEnd) { // $ExpectType HTMLElement @@ -6489,7 +6489,7 @@ function EffectsOptions() { }); } -function _Event() { +function JQuery_Event() { function call_signature() { // $ExpectType Event & Coordinates $.Event('keydown', $('p').offset()); @@ -6511,7 +6511,7 @@ function _Event() { } } -function jqXHR() { +function JQuery_jqXHR() { const p: JQuery.jqXHR = {} as any; function always() { @@ -6755,23 +6755,31 @@ function jqXHR() { } function _catch() { - // $ExpectType Promise3 - $.ajax('/echo').catch((jqXHR, textStatus, errorThrown) => { - // $ExpectType jqXHR - jqXHR; - // $ExpectType ErrorTextStatus - textStatus; - // $ExpectType string - errorThrown; - }); + { + const a = $.ajax('/echo').catch((jqXHR, textStatus, errorThrown) => { + // $ExpectType jqXHR + jqXHR; + // $ExpectType ErrorTextStatus + textStatus; + // $ExpectType string + errorThrown; + }); + // $ExpectType PromiseBase + a; + const b: JQuery.Promise3 = a; + } } function compatibleWithPromise(): Promise { return p; } + + function compatibleWithJQueryPromise(): JQuery.Promise { + return p; + } } -function Promise3() { +function JQuery_Promise3() { interface I1 { kind: 'I1'; } interface I2 { kind: 'I2'; } interface I3 { kind: 'I3'; } @@ -7137,28 +7145,38 @@ function Promise3() { } function _catch() { - // $ExpectType Promise3 - $.ajax('/echo/json').catch(() => { - return 1; - }); + { + const a = $.ajax('/echo/json').catch(() => { + return 1; + }); + // $ExpectType PromiseBase + a; + const b: JQuery.Promise3 = a; + } + { + const a = $.ajax('/echo/json').catch(() => { + return t1; + }); + // $ExpectType PromiseBase + a; + const b: JQuery.Promise3 = a; + } + { + const a = $.ajax('/echo/json').catch(() => { + return $.ajax('/echo/json'); + }); + // $ExpectType PromiseBase, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never, never, never, never> + a; + const b: JQuery.Promise3, never, JQuery.Ajax.SuccessTextStatus, JQuery.Ajax.ErrorTextStatus, never, JQuery.jqXHR, string, never> = a; + } - // $ExpectType Promise3 - $.ajax('/echo/json').catch(() => { - return t1; - }); - - // $ExpectType Promise3, never, SuccessTextStatus, ErrorTextStatus, never, jqXHR, string, never> - $.ajax('/echo/json').catch(() => { - return $.ajax('/echo/json'); - }); - - // $ExpectType Promise3 + // $ExpectType PromiseBase $.ajax('/echo/json').catch(undefined); - // $ExpectType Promise3 + // $ExpectType PromiseBase $.ajax('/echo/json').catch(null); - // $ExpectType Promise3 + // $ExpectType PromiseBase $.ajax('/echo/json').catch(); } @@ -7176,9 +7194,13 @@ function Promise3() { function compatibleWithPromise(): Promise { return p; } + + function compatibleWithJQueryPromise(): JQuery.Promise { + return p; + } } -function Promise2(p: JQuery.Promise2) { +function JQuery_Promise2(p: JQuery.Promise2) { function then() { p.then((a, b) => { a; // $ExpectType string @@ -7315,9 +7337,13 @@ function Promise2(p: JQuery.Promise2 { return p; } + + function compatibleWithJQueryPromise(): JQuery.Promise { + return p; + } } -function _Promise(p: JQuery.Promise) { +function JQuery_Promise(p: JQuery.Promise) { function then() { p.then((a) => { a; // $ExpectType string @@ -7432,3 +7458,19 @@ function _Promise(p: JQuery.Promise) { return p; } } + +function JQuery_Deferred() { + interface I1 { kind: 'I1'; } + interface I2 { kind: 'I2'; } + interface I3 { kind: 'I3'; } + + const d1 = $.Deferred(); + + function promise() { + const target: I1 = {} as any; + + d1.promise(target); // $ExpectType Promise & I1 + + d1.promise(); // $ExpectType Promise + } +}