From ebbf64f76b0b702ca0591ffa43c3586f994c9e43 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 20 Nov 2018 15:56:58 -0800 Subject: [PATCH] Work around 3.2 inference change using overloads Notes: 1. I only duplicated overloads to get 2 * n instead of 2 * n overloads needed to support every combination of constructor function/other. 2. I deleted the duplicate R vs U | R overloads for `catch` since they were equivalent; I don't think the R overloads were ever used. 3. The original overload is now Constructor | CatchFilter; I haven't figured out why yet, but normal classes don't use the new Constructor-only overload. Only constructor functions do. --- types/bluebird/bluebird-tests.ts | 8 +- types/bluebird/index.d.ts | 277 +++++++++++++++++++++---------- 2 files changed, 190 insertions(+), 95 deletions(-) diff --git a/types/bluebird/bluebird-tests.ts b/types/bluebird/bluebird-tests.ts index 8fe5db7538..05ac5cc19c 100644 --- a/types/bluebird/bluebird-tests.ts +++ b/types/bluebird/bluebird-tests.ts @@ -313,7 +313,7 @@ fooOrBarProm = fooProm.caught((error: any) => { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // $ExpectType Bluebird -fooProm.catch(Error as { new(message?: string): Error }, (reason: any) => { +fooProm.catch(Error, (reason: any) => { return; }); // $ExpectType Bluebird @@ -321,7 +321,7 @@ fooProm.catch(Promise.CancellationError, (reason: any) => { return; }); // $ExpectType Bluebird -fooProm.caught(Error as { new(message?: string): Error }, (reason: any) => { +fooProm.caught(Error, (reason: any) => { return; }); // $ExpectType Bluebird @@ -329,13 +329,13 @@ fooProm.caught(Promise.CancellationError, (reason: any) => { return; }); -fooOrBarProm = fooProm.catch(Error as { new(message?: string): Error }, (reason: any) => { +fooOrBarProm = fooProm.catch(Error, (reason: any) => { return bar; }); fooOrBarProm = fooProm.catch(Promise.CancellationError, (reason: any) => { return bar; }); -fooOrBarProm = fooProm.caught(Error as { new(message?: string): Error }, (reason: any) => { +fooOrBarProm = fooProm.caught(Error, (reason: any) => { return bar; }); fooOrBarProm = fooProm.caught(Promise.CancellationError, (reason: any) => { diff --git a/types/bluebird/index.d.ts b/types/bluebird/index.d.ts index 091dbe6e7a..1f8768498a 100644 --- a/types/bluebird/index.d.ts +++ b/types/bluebird/index.d.ts @@ -35,7 +35,8 @@ * THE SOFTWARE. */ -type CatchFilter = (new (...args: any[]) => E) | ((error: E) => boolean) | (object & E); +type Constructor = new (...args: any[]) => E +type CatchFilter = ((error: E) => boolean) | (object & E); type IterableItem = R extends Iterable ? U : never; type IterableOrNever = Extract>; type Resolvable = R | PromiseLike; @@ -85,69 +86,73 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { * * Alias `.caught();` for compatibility with earlier ECMAScript version. */ - catch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, - filter5: CatchFilter, - onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, - ): Bluebird; catch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, - filter5: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, + filter5: Constructor, onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, ): Bluebird; - catch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, - onReject: (error: E1 | E2 | E3 | E4) => Resolvable, - ): Bluebird; + catch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, + filter5: Constructor | CatchFilter, + onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, + ): Bluebird; catch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, + onReject: (error: E1 | E2 | E3 | E4) => Resolvable, + ): Bluebird; + + catch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3 | E4) => Resolvable, ): Bluebird; - catch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - onReject: (error: E1 | E2 | E3) => Resolvable, - ): Bluebird; catch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + onReject: (error: E1 | E2 | E3) => Resolvable, + ): Bluebird; + + catch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3) => Resolvable, ): Bluebird; - catch( - filter1: CatchFilter, - filter2: CatchFilter, - onReject: (error: E1 | E2) => Resolvable, - ): Bluebird; catch( - filter1: CatchFilter, - filter2: CatchFilter, + filter1: Constructor, + filter2: Constructor, + onReject: (error: E1 | E2) => Resolvable, + ): Bluebird; + + catch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, onReject: (error: E1 | E2) => Resolvable, ): Bluebird; - catch( - filter1: CatchFilter, - onReject: (error: E1) => Resolvable, - ): Bluebird; catch( - filter1: CatchFilter, + filter1: Constructor, + onReject: (error: E1) => Resolvable, + ): Bluebird; + + catch( + filter1: Constructor | CatchFilter, onReject: (error: E1) => Resolvable, ): Bluebird; @@ -201,33 +206,63 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { tapCatch(onReject: (error?: any) => Resolvable): Bluebird; tapCatch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, - filter5: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, + filter5: Constructor, + onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, + ): Bluebird; + tapCatch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, + filter5: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, ): Bluebird; tapCatch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, + onReject: (error: E1 | E2 | E3 | E4) => Resolvable, + ): Bluebird; + tapCatch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3 | E4) => Resolvable, ): Bluebird; tapCatch( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + onReject: (error: E1 | E2 | E3) => Resolvable, + ): Bluebird; + tapCatch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3) => Resolvable, ): Bluebird; tapCatch( - filter1: CatchFilter, - filter2: CatchFilter, + filter1: Constructor, + filter2: Constructor, + onReject: (error: E1 | E2) => Resolvable, + ): Bluebird; + tapCatch( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, onReject: (error: E1 | E2) => Resolvable, ): Bluebird; tapCatch( - filter1: CatchFilter, + filter1: Constructor, + onReject: (error: E1) => Resolvable, + ): Bluebird; + tapCatch( + filter1: Constructor | CatchFilter, onReject: (error: E1) => Resolvable, ): Bluebird; @@ -376,33 +411,63 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { // No need to be specific about Error types in these overrides, since there's no handler function catchReturn( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, - filter5: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, + filter5: Constructor, value: U, ): Bluebird; catchReturn( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, + filter5: Constructor | CatchFilter, value: U, ): Bluebird; catchReturn( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, value: U, ): Bluebird; catchReturn( - filter1: CatchFilter, - filter2: CatchFilter, + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, value: U, ): Bluebird; catchReturn( - filter1: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + value: U, + ): Bluebird; + catchReturn( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + value: U, + ): Bluebird; + catchReturn( + filter1: Constructor, + filter2: Constructor, + value: U, + ): Bluebird; + catchReturn( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + value: U, + ): Bluebird; + catchReturn( + filter1: Constructor, + value: U, + ): Bluebird; + catchReturn( + filter1: Constructor | CatchFilter, value: U, ): Bluebird; @@ -420,33 +485,63 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { // No need to be specific about Error types in these overrides, since there's no handler function catchThrow( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, - filter5: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, + filter5: Constructor, reason: Error, ): Bluebird; catchThrow( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, - filter4: CatchFilter, + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, + filter5: Constructor | CatchFilter, reason: Error, ): Bluebird; catchThrow( - filter1: CatchFilter, - filter2: CatchFilter, - filter3: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + filter4: Constructor, reason: Error, ): Bluebird; catchThrow( - filter1: CatchFilter, - filter2: CatchFilter, + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + filter4: Constructor | CatchFilter, reason: Error, ): Bluebird; catchThrow( - filter1: CatchFilter, + filter1: Constructor, + filter2: Constructor, + filter3: Constructor, + reason: Error, + ): Bluebird; + catchThrow( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + filter3: Constructor | CatchFilter, + reason: Error, + ): Bluebird; + catchThrow( + filter1: Constructor, + filter2: Constructor, + reason: Error, + ): Bluebird; + catchThrow( + filter1: Constructor | CatchFilter, + filter2: Constructor | CatchFilter, + reason: Error, + ): Bluebird; + catchThrow( + filter1: Constructor, + reason: Error, + ): Bluebird; + catchThrow( + filter1: Constructor | CatchFilter, reason: Error, ): Bluebird;