From 05d4224c2f2c8fe2edb6d912dcb5bbd681014be6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Mon, 25 Feb 2019 14:11:01 -0800 Subject: [PATCH] Restore covariant comparison to bluebird's Promise Now that typescript@next checks variance more strictly for conditional types, bluebird's Promise is invariant. This PR removes conditional types from bluebird so that Promise compares covariantly again. Note that I never figured out how to do this for `call`, so it returns `Bluebird` right now. I'm still working on that. --- types/bluebird/index.d.ts | 46 ++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/types/bluebird/index.d.ts b/types/bluebird/index.d.ts index 24c2b79a32..e8c6dbf5a2 100644 --- a/types/bluebird/index.d.ts +++ b/types/bluebird/index.d.ts @@ -37,8 +37,6 @@ 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; type IterateFunction = (item: T, index: number, arrayLength: number) => Resolvable; @@ -352,7 +350,7 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { * }); * */ - call(propertyName: U, ...args: any[]): Bluebird any ? ReturnType : never>; + call(propertyName: U, ...args: any[]): Bluebird; /** * This is a convenience method for doing: @@ -562,12 +560,17 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { /** * Like calling `.then`, but the fulfillment value or rejection reason is assumed to be an array, which is flattened to the formal parameters of the handlers. */ - spread(fulfilledHandler: (...values: Array>) => Resolvable): Bluebird; + spread(this: Bluebird>, fulfilledHandler: (...values: Array) => Resolvable): Bluebird; /** * Same as calling `Promise.all(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - all(): Bluebird>; + all(this: Bluebird>): Bluebird; + + /** + * Same as calling `Promise.all(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + all(): Bluebird; /** * Same as calling `Promise.props(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. @@ -578,42 +581,59 @@ declare class Bluebird implements PromiseLike, Bluebird.Inspection { /** * Same as calling `Promise.any(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - any(): Bluebird>; + any(this: Bluebird>): Bluebird; + + /** + * Same as calling `Promise.any(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + any(): Bluebird; /** + * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - some(count: number): Bluebird>; + some(this: Bluebird>, count: number): Bluebird; + + /** + * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + some(count: number): Bluebird; /** * Same as calling `Promise.race(thisPromise, count)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - race(): Bluebird>; + race(this: Bluebird>): Bluebird; + + /** + * Same as calling `Promise.race(thisPromise, count)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. + */ + race(): Bluebird; /** * Same as calling `Bluebird.map(thisPromise, mapper)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - map(mapper: IterateFunction, U>, options?: Bluebird.ConcurrencyOption): Bluebird ? U[] : never>; + map(this: Bluebird>, mapper: IterateFunction, options?: Bluebird.ConcurrencyOption): Bluebird; /** * Same as calling `Promise.reduce(thisPromise, Function reducer, initialValue)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - reduce(reducer: (memo: U, item: IterableItem, index: number, arrayLength: number) => Resolvable, initialValue?: U): Bluebird ? U : never>; + reduce(this: Bluebird>, reducer: (memo: U, item: Q, index: number, arrayLength: number) => Resolvable, initialValue?: U): Bluebird; /** * Same as calling ``Promise.filter(thisPromise, filterer)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - filter(filterer: IterateFunction, boolean>, options?: Bluebird.ConcurrencyOption): Bluebird>; + filter(this: Bluebird>, filterer: IterateFunction, options?: Bluebird.ConcurrencyOption): Bluebird; /** * Same as calling ``Bluebird.each(thisPromise, iterator)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - each(iterator: IterateFunction, any>): Bluebird>; + each(this: Bluebird>, iterator: IterateFunction): Bluebird; /** * Same as calling ``Bluebird.mapSeries(thisPromise, iterator)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too. */ - mapSeries(iterator: IterateFunction, U>): Bluebird ? U[] : never>; + mapSeries(this: Bluebird>, iterator: IterateFunction): Bluebird; /** * Cancel this `promise`. Will not do anything if this promise is already settled or if the cancellation feature has not been enabled