UNPKG

16.4 kBTypeScriptView Raw
1// Type definitions for bluebird 3.5
2// Project: https://github.com/kaatt/bluebird-global
3// Definitions by: d-ph <https://github.com/d-ph>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 3.2
6
7/*
8 * 1. Why use `bluebird-global` instead of `bluebird`?
9 *
10 * If you want to leverage the fact, that bluebird polyfills the global Promise in the browser, then
11 * you need to tell TypeScript about this. The following declaration file does exactly that.
12 *
13 * 1.1. Why you might not want to use `bluebird-global` instead of `bluebird`.
14 *
15 * Because of how these typings tell TypeScript about bluebird's Promise methods, it is not
16 * possible to cast global Promises to Bluebird promises in your code. In other words, you won't
17 * be able to do the following (even though it's possible at the runtime):
18 *
19 * let bluebirdPromise: Bluebird<string> = new Promise<string>(() => { return 'Lorem ipsum'; });
20 *
21 * If you need to, you can walk-around this by constructing a new Bluebird promise over an instance
22 * of the global Promise, like so:
23 *
24 * let bluebirdPromise: Bluebird<string> = Bluebird.resolve(
25 * new Promise<string>(() => { return 'Lorem ipsum'; })
26 * );
27 *
28 * So the bottom line is: if you use these typings, then be mindful when you try to mix the global
29 * Promises with the Bluebird promises. You can avoid this problem by just settling on using either
30 * of them and not both of them at the same time.
31 *
32 * 1.2. Further limitations of `bluebird-global` typings: the return type of Bluebird's methods.
33 *
34 * Due to the fact of how bluebird-specific methods are exposed on the global Promise, the
35 * return type of those methods is Bluebird<T> instead of Promise<T>. This is relevant in the
36 * following case:
37 *
38 * function createDelayedPromise(): Promise<void> {
39 * return Promise.delay(250);
40 * }
41 *
42 * Since Promise.delay() returns a Bluebird<void> and the function is typed to return a Promise<void>,
43 * an implicit cast is performed from Bluebird<void> to Promise<void>. And since an instance
44 * of Bluebird isn't and instance of Promise (due to how `bluebird-global` works), this implicit
45 * cast fails to compile. In order to walk-around this problem, the following explicit cast should
46 * be used:
47 *
48 * function createDelayedPromise(): Promise<void> {
49 * return <Promise<void>> Promise.delay(250);
50 * }
51 *
52 * 2. How to use it?
53 *
54 * It should just work, but there are a couple of points to be wary about:
55 *
56 * a) If you already use `compilerOptions.types` in your `tsconfig.json`, then add `bluebird-global`
57 * to the list:
58 *
59 * {
60 * "compilerOptions": {
61 * "types": [
62 * (other types ...)
63 *
64 * "bluebird-global"
65 * ],
66 * }
67 * }
68 *
69 * b) Be aware, that you still need to get the global Promise symbol to be replaced with bluebird.js
70 * in the runtime. Do this by either importing bluebird.js via a `<script />` tag in your html or
71 * via importing it in your js entry file AND assigning it to the global Promise symbol.
72 *
73 * c) if you're `--target`ing "es5", then you'll need to include the "es2015.iterable" lib typings to
74 * let the bluebird.d.ts compile (requirement for Map, Iterable & friends). This is only for the
75 * compile-time, since bluebird doesn't require these es6 features to be present in the runtime.
76 * To summarise: when targeting "es5", make sure you have the follwing "lib" config in your tsconfig.json:
77 *
78 * {
79 * "compilerOptions": {
80 * "lib": [
81 * "es5",
82 * "es2015.iterable",
83 * "dom"
84 * (more, if you need...)
85 * ],
86 * }
87 * }
88 *
89 * 3. Why so much effort?
90 *
91 * If a promise-polyfilling library wants to play nicely with TypeScript, it needs to augment
92 * the Promise<T> and PromiseConstructor interfaces defined in the standard ts library.
93 * For various reasons this couldn't be done in The `bluebird` typings.
94 *
95 * 4. Contributors: After changing this file please manually test these cases (via altering ./tsconfig.json ):
96 * a. target es5, with the following `lib` keys: "es5", "es2015.iterable", "dom"
97 * b. target es6, no `lib` key
98 * c. target es5, latest "es20xx", e.g. "es2017"
99 * d. target es6, latest "es20xx", e.g. "es2017"
100 */
101
102/*
103 * @todo When dropping TS 2.x support, uncomment the following triple-slash directives and
104 * remove the copy&paste from this file (marked with #std-lib-copy&paste-to-remove)
105 *
106 * TS 2.x support should be dropped once bluebird's typings stop compiling on 2.x (i.e.
107 * once bluebird's typings stop supporting TS 2.x)
108 */
109/* /// <reference lib="es5" /> */
110/* /// <reference lib="es2015.promise" /> */
111/* /// <reference lib="es2018.promise" /> */
112
113import Bluebird = require("bluebird");
114
115declare global {
116 type IterateFunction<T, R> = (item: T, index: number, arrayLength: number) => (R | PromiseLike<R>);
117 /*
118 * Patch all instance method
119 */
120 interface Promise<T> {
121 all(this: Promise<Iterable<{}>>): Bluebird<T>;
122 all(): Bluebird<never>;
123 any<Q>(this: Promise<T & Iterable<Q>>): Bluebird<Q>;
124 any(): Bluebird<never>;
125 asCallback: Bluebird<T>["asCallback"];
126 bind: Bluebird<T>["bind"];
127 call: Bluebird<T>["call"];
128 cancel: Bluebird<T>["cancel"];
129 // catch: Bluebird<T>["catch"]; // Provided by lib.es5.d.ts
130 caught: Bluebird<T>["caught"];
131 delay: Bluebird<T>["delay"];
132 disposer: Bluebird<T>["disposer"];
133 done: Bluebird<T>["done"];
134 each<Q>(this: Promise<T & Iterable<Q>>, iterator: IterateFunction<Q, any>): Bluebird<T>;
135 error: Bluebird<T>["error"];
136 filter<Q>(this: Promise<T & Iterable<Q>>, filterer: IterateFunction<Q, boolean>, options?: Bluebird.ConcurrencyOption): Bluebird<T>;
137 // finally: Bluebird<T>["finally"]; // Provided by lib.es2018.promise.d.ts
138 get: Bluebird<T>["get"];
139 isCancelled: Bluebird<T>["isCancelled"];
140 isFulfilled: Bluebird<T>["isFulfilled"];
141 isPending: Bluebird<T>["isPending"];
142 isRejected: Bluebird<T>["isRejected"];
143 isResolved: Bluebird<T>["isResolved"];
144 lastly: Bluebird<T>["lastly"];
145 map<U, Q>(this: Promise<T & Iterable<Q>>, mapper: IterateFunction<Q, U>, options?: Bluebird.ConcurrencyOption): Bluebird<U[]>;
146 mapSeries<U, Q>(this: Promise<T & Iterable<Q>>, iterator: IterateFunction<Q, U>): Bluebird<U[]>;
147 nodeify: Bluebird<T>["nodeify"];
148 props: Bluebird<T>["props"];
149 race<Q>(this: Promise<T & Iterable<Q>>): Bluebird<Q>;
150 race(): Bluebird<never>;
151 reason: Bluebird<T>["reason"];
152 reduce<U, Q>(this: Promise<T & Iterable<Q>>, reducer: (memo: U, item: Q, index: number, arrayLength: number) => (U | PromiseLike<U>), initialValue?: U): Bluebird<U>;
153 reflect: Bluebird<T>["reflect"];
154 return: Bluebird<T>["return"];
155 some(this: Promise<Iterable<{}>>, count: number): Bluebird<T>;
156 spread<U, Q>(this: Bluebird<T & Iterable<Q>>, fulfilledHandler: (...values: Q[]) => (U | PromiseLike<U>)): Bluebird<U>;
157 suppressUnhandledRejections: Bluebird<T>["suppressUnhandledRejections"];
158 tap: Bluebird<T>["tap"];
159 tapCatch: Bluebird<T>["tapCatch"];
160 // then: Bluebird<T>["then"]; // Provided by lib.es5.d.ts
161 thenReturn: Bluebird<T>["thenReturn"];
162 thenThrow: Bluebird<T>["thenThrow"];
163 catchReturn: Bluebird<T>["catchReturn"];
164 catchThrow: Bluebird<T>["catchThrow"];
165 throw: Bluebird<T>["throw"];
166 timeout: Bluebird<T>["timeout"];
167 toJSON: Bluebird<T>["toJSON"];
168 toString: Bluebird<T>["toString"];
169 value: Bluebird<T>["value"];
170
171 /*
172 * Copy&paste ::then and ::catch from lib.es5.promise.d.ts, because Bluebird's typings are not
173 * in line with the standard lib.
174 *
175 * #std-lib-copy&paste-to-remove
176 *
177 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
178 */
179 then<TResult1 = T, TResult2 = never>(
180 onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
181 onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
182 ): Promise<TResult1 | TResult2>;
183 catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<T | TResult>;
184
185 /*
186 * TypeScript disallows adding overrides via `catch: typeof Bluebird.prototype.catch`. Copy&paste them then.
187 *
188 * @todo Duplication of code is never ideal. See whether there's a better way of achieving this.
189 */
190 catch(predicate: (error: any) => boolean, onReject: (error: any) => T | PromiseLike<T> | void | PromiseLike<void>): Bluebird<T>;
191 catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => U | PromiseLike<U>): Bluebird<U | T>;
192 catch<E extends Error>(ErrorClass: new (...args: any[]) => E, onReject: (error: E) => T | PromiseLike<T> | void | PromiseLike<void>): Bluebird<T>;
193 catch<E extends Error, U>(ErrorClass: new (...args: any[]) => E, onReject: (error: E) => U | PromiseLike<U>): Bluebird<U | T>;
194 catch(predicate: Object, onReject: (error: any) => T | PromiseLike<T> | void | PromiseLike<void>): Bluebird<T>;
195 catch<U>(predicate: Object, onReject: (error: any) => U | PromiseLike<U>): Bluebird<U | T>;
196
197 /*
198 * See comments above `then` for the reason why this is needed. Taken from es2018.promise.d.ts.
199 *
200 * #std-lib-copy&paste-to-remove
201 *
202 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
203 */
204 finally(onfinally?: (() => void) | null): Promise<T>;
205 }
206
207 /*
208 * Patch all static methods and the constructor
209 */
210 interface PromiseConstructor {
211 new <T>(callback: (resolve: (thenableOrResult?: T | PromiseLike<T>) => void, reject: (error?: any) => void, onCancel?: (callback: () => void) => void) => void): Promise<T>;
212
213 // all: typeof Bluebird.all; // Provided by lib.es2015.d.ts
214 any: typeof Bluebird.any;
215 attempt: typeof Bluebird.attempt;
216 bind: typeof Bluebird.bind;
217 cast: typeof Bluebird.cast;
218 config: typeof Bluebird.config;
219 coroutine: typeof Bluebird.coroutine;
220 defer: typeof Bluebird.defer;
221 delay: typeof Bluebird.delay;
222 each: typeof Bluebird.each;
223 filter: typeof Bluebird.filter;
224 fromCallback: typeof Bluebird.fromCallback;
225 fromNode: typeof Bluebird.fromNode;
226 is: typeof Bluebird.is;
227 join: typeof Bluebird.join;
228 longStackTraces: typeof Bluebird.longStackTraces;
229 map: typeof Bluebird.map;
230 mapSeries: typeof Bluebird.mapSeries;
231 method: typeof Bluebird.method;
232 onPossiblyUnhandledRejection: typeof Bluebird.onPossiblyUnhandledRejection;
233 promisify: typeof Bluebird.promisify;
234 promisifyAll: typeof Bluebird.promisifyAll;
235 props: typeof Bluebird.props;
236 // race: typeof Bluebird.race; // Provided by lib.es2015.d.ts
237 reduce: typeof Bluebird.reduce;
238 // reject: typeof Bluebird.reject; // Provided by lib.es2015.d.ts
239 // resolve: typeof Bluebird.resolve; // Provided by lib.es2015.d.ts
240 some: typeof Bluebird.some;
241 try: typeof Bluebird.try;
242 using: typeof Bluebird.using;
243
244 /*
245 * Copy&paste from lib.es2015.promise.d.ts, because Bluebird's typings are not in line with the standard lib.
246 *
247 * #std-lib-copy&paste-to-remove
248 *
249 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
250 */
251 all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
252 all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
253 all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
254 all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
255 all<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
256 all<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>;
257 all<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): Promise<[T1, T2, T3, T4]>;
258 all<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
259 all<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
260 all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
261 race<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
262 race<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
263 race<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
264 race<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
265 race<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<T1 | T2 | T3 | T4 | T5 | T6>;
266 race<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): Promise<T1 | T2 | T3 | T4 | T5>;
267 race<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<T1 | T2 | T3 | T4>;
268 race<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<T1 | T2 | T3>;
269 race<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<T1 | T2>;
270 race<T>(values: (T | PromiseLike<T>)[]): Promise<T>;
271 reject(reason: any): Promise<never>;
272 reject<T>(reason: any): Promise<T>;
273 resolve<T>(value: T | PromiseLike<T>): Promise<T>;
274 resolve(): Promise<void>;
275 }
276
277 /*
278 * Declare the `Promise` variable. This is needed for es5 only and is a no-op for all other targets.
279 *
280 * #std-lib-copy&paste-to-remove
281 *
282 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
283 */
284 var Promise: PromiseConstructor;
285}
286
\No newline at end of file