UNPKG

18.7 kBTypeScriptView Raw
1/*
2 * 1. Why use `bluebird-global` instead of `bluebird`?
3 *
4 * If you want to leverage the fact, that bluebird polyfills the global Promise in the browser, then
5 * you need to tell TypeScript about this. The following declaration file does exactly that.
6 *
7 * 1.1. Why you might not want to use `bluebird-global` instead of `bluebird`.
8 *
9 * Because of how these typings tell TypeScript about bluebird's Promise methods, it is not
10 * possible to cast global Promises to Bluebird promises in your code. In other words, you won't
11 * be able to do the following (even though it's possible at the runtime):
12 *
13 * let bluebirdPromise: Bluebird<string> = new Promise<string>(() => { return 'Lorem ipsum'; });
14 *
15 * If you need to, you can walk-around this by constructing a new Bluebird promise over an instance
16 * of the global Promise, like so:
17 *
18 * let bluebirdPromise: Bluebird<string> = Bluebird.resolve(
19 * new Promise<string>(() => { return 'Lorem ipsum'; })
20 * );
21 *
22 * So the bottom line is: if you use these typings, then be mindful when you try to mix the global
23 * Promises with the Bluebird promises. You can avoid this problem by just settling on using either
24 * of them and not both of them at the same time.
25 *
26 * 1.2. Further limitations of `bluebird-global` typings: the return type of Bluebird's methods.
27 *
28 * Due to the fact of how bluebird-specific methods are exposed on the global Promise, the
29 * return type of those methods is Bluebird<T> instead of Promise<T>. This is relevant in the
30 * following case:
31 *
32 * function createDelayedPromise(): Promise<void> {
33 * return Promise.delay(250);
34 * }
35 *
36 * Since Promise.delay() returns a Bluebird<void> and the function is typed to return a Promise<void>,
37 * an implicit cast is performed from Bluebird<void> to Promise<void>. And since an instance
38 * of Bluebird isn't and instance of Promise (due to how `bluebird-global` works), this implicit
39 * cast fails to compile. In order to walk-around this problem, the following explicit cast should
40 * be used:
41 *
42 * function createDelayedPromise(): Promise<void> {
43 * return <Promise<void>> Promise.delay(250);
44 * }
45 *
46 * 2. How to use it?
47 *
48 * It should just work, but there are a couple of points to be wary about:
49 *
50 * a) If you already use `compilerOptions.types` in your `tsconfig.json`, then add `bluebird-global`
51 * to the list:
52 *
53 * {
54 * "compilerOptions": {
55 * "types": [
56 * (other types ...)
57 *
58 * "bluebird-global"
59 * ],
60 * }
61 * }
62 *
63 * b) Be aware, that you still need to get the global Promise symbol to be replaced with bluebird.js
64 * in the runtime. Do this by either importing bluebird.js via a `<script />` tag in your html or
65 * via importing it in your js entry file AND assigning it to the global Promise symbol.
66 *
67 * c) if you're `--target`ing "es5", then you'll need to include the "es2015.iterable" lib typings to
68 * let the bluebird.d.ts compile (requirement for Map, Iterable & friends). This is only for the
69 * compile-time, since bluebird doesn't require these es6 features to be present in the runtime.
70 * To summarise: when targeting "es5", make sure you have the follwing "lib" config in your tsconfig.json:
71 *
72 * {
73 * "compilerOptions": {
74 * "lib": [
75 * "es5",
76 * "es2015.iterable",
77 * "dom"
78 * (more, if you need...)
79 * ],
80 * }
81 * }
82 *
83 * 3. Why so much effort?
84 *
85 * If a promise-polyfilling library wants to play nicely with TypeScript, it needs to augment
86 * the Promise<T> and PromiseConstructor interfaces defined in the standard ts library.
87 * For various reasons this couldn't be done in The `bluebird` typings.
88 *
89 * 4. Contributors: After changing this file please manually test these cases (via altering ./tsconfig.json ):
90 * a. target es5, with the following `lib` keys: "es5", "es2015.iterable", "dom"
91 * b. target es6, no `lib` key
92 * c. target es5, latest "es20xx", e.g. "es2017"
93 * d. target es6, latest "es20xx", e.g. "es2017"
94 */
95
96/*
97 * @todo When dropping TS 2.x support, uncomment the following triple-slash directives and
98 * remove the copy&paste from this file (marked with #std-lib-copy&paste-to-remove)
99 *
100 * TS 2.x support should be dropped once bluebird's typings stop compiling on 2.x (i.e.
101 * once bluebird's typings stop supporting TS 2.x)
102 */
103/* /// <reference lib="es5" /> */
104/* /// <reference lib="es2015.promise" /> */
105/* /// <reference lib="es2018.promise" /> */
106
107import Bluebird = require("bluebird");
108
109declare global {
110 type IterateFunction<T, R> = (item: T, index: number, arrayLength: number) => R | PromiseLike<R>;
111 /*
112 * Patch all instance method
113 */
114 interface Promise<T> {
115 all(this: Promise<Iterable<{}>>): Bluebird<T>;
116 all(): Bluebird<never>;
117 any<Q>(this: Promise<T & Iterable<Q>>): Bluebird<Q>;
118 any(): Bluebird<never>;
119 asCallback: Bluebird<T>["asCallback"];
120 bind: Bluebird<T>["bind"];
121 call: Bluebird<T>["call"];
122 cancel: Bluebird<T>["cancel"];
123 // catch: Bluebird<T>["catch"]; // Provided by lib.es5.d.ts
124 caught: Bluebird<T>["caught"];
125 delay: Bluebird<T>["delay"];
126 disposer: Bluebird<T>["disposer"];
127 done: Bluebird<T>["done"];
128 each<Q>(this: Promise<T & Iterable<Q>>, iterator: IterateFunction<Q, any>): Bluebird<T>;
129 error: Bluebird<T>["error"];
130 filter<Q>(
131 this: Promise<T & Iterable<Q>>,
132 filterer: IterateFunction<Q, boolean>,
133 options?: Bluebird.ConcurrencyOption,
134 ): Bluebird<T>;
135 // finally: Bluebird<T>["finally"]; // Provided by lib.es2018.promise.d.ts
136 get: Bluebird<T>["get"];
137 isCancelled: Bluebird<T>["isCancelled"];
138 isFulfilled: Bluebird<T>["isFulfilled"];
139 isPending: Bluebird<T>["isPending"];
140 isRejected: Bluebird<T>["isRejected"];
141 isResolved: Bluebird<T>["isResolved"];
142 lastly: Bluebird<T>["lastly"];
143 map<U, Q>(
144 this: Promise<T & Iterable<Q>>,
145 mapper: IterateFunction<Q, U>,
146 options?: Bluebird.ConcurrencyOption,
147 ): Bluebird<U[]>;
148 mapSeries<U, Q>(this: Promise<T & Iterable<Q>>, iterator: IterateFunction<Q, U>): Bluebird<U[]>;
149 nodeify: Bluebird<T>["nodeify"];
150 props: Bluebird<T>["props"];
151 race<Q>(this: Promise<T & Iterable<Q>>): Bluebird<Q>;
152 race(): Bluebird<never>;
153 reason: Bluebird<T>["reason"];
154 reduce<U, Q>(
155 this: Promise<T & Iterable<Q>>,
156 reducer: (memo: U, item: Q, index: number, arrayLength: number) => U | PromiseLike<U>,
157 initialValue?: U,
158 ): Bluebird<U>;
159 reflect: Bluebird<T>["reflect"];
160 return: Bluebird<T>["return"];
161 some(this: Promise<Iterable<{}>>, count: number): Bluebird<T>;
162 spread<U, Q>(
163 this: Bluebird<T & Iterable<Q>>,
164 fulfilledHandler: (...values: Q[]) => U | PromiseLike<U>,
165 ): Bluebird<U>;
166 suppressUnhandledRejections: Bluebird<T>["suppressUnhandledRejections"];
167 tap: Bluebird<T>["tap"];
168 tapCatch: Bluebird<T>["tapCatch"];
169 // then: Bluebird<T>["then"]; // Provided by lib.es5.d.ts
170 thenReturn: Bluebird<T>["thenReturn"];
171 thenThrow: Bluebird<T>["thenThrow"];
172 catchReturn: Bluebird<T>["catchReturn"];
173 catchThrow: Bluebird<T>["catchThrow"];
174 throw: Bluebird<T>["throw"];
175 timeout: Bluebird<T>["timeout"];
176 toJSON: Bluebird<T>["toJSON"];
177 toString: Bluebird<T>["toString"];
178 value: Bluebird<T>["value"];
179
180 /*
181 * Copy&paste ::then and ::catch from lib.es5.promise.d.ts, because Bluebird's typings are not
182 * in line with the standard lib.
183 *
184 * #std-lib-copy&paste-to-remove
185 *
186 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
187 */
188 then<TResult1 = T, TResult2 = never>(
189 onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
190 onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
191 ): Promise<TResult1 | TResult2>;
192 catch<TResult = never>(
193 onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
194 ): Promise<T | TResult>;
195
196 /*
197 * TypeScript disallows adding overrides via `catch: typeof Bluebird.prototype.catch`. Copy&paste them then.
198 *
199 * @todo Duplication of code is never ideal. See whether there's a better way of achieving this.
200 */
201 catch(
202 predicate: (error: any) => boolean,
203 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
204 onReject: (error: any) => T | PromiseLike<T> | void | PromiseLike<void>,
205 ): Bluebird<T>;
206 catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => U | PromiseLike<U>): Bluebird<U | T>;
207 catch<E extends Error>(
208 ErrorClass: new(...args: any[]) => E,
209 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
210 onReject: (error: E) => T | PromiseLike<T> | void | PromiseLike<void>,
211 ): Bluebird<T>;
212 catch<E extends Error, U>(
213 ErrorClass: new(...args: any[]) => E,
214 onReject: (error: E) => U | PromiseLike<U>,
215 ): Bluebird<U | T>;
216 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
217 catch(predicate: Object, onReject: (error: any) => T | PromiseLike<T> | void | PromiseLike<void>): Bluebird<T>;
218 catch<U>(predicate: Object, onReject: (error: any) => U | PromiseLike<U>): Bluebird<U | T>;
219
220 /*
221 * See comments above `then` for the reason why this is needed. Taken from es2018.promise.d.ts.
222 *
223 * #std-lib-copy&paste-to-remove
224 *
225 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
226 */
227 finally(onfinally?: (() => void) | null): Promise<T>;
228 }
229
230 /*
231 * Patch all static methods and the constructor
232 */
233 interface PromiseConstructor {
234 new<T>(
235 callback: (
236 resolve: (thenableOrResult?: T | PromiseLike<T>) => void,
237 reject: (error?: any) => void,
238 onCancel?: (callback: () => void) => void,
239 ) => void,
240 ): Promise<T>;
241
242 // all: typeof Bluebird.all; // Provided by lib.es2015.d.ts
243 any: typeof Bluebird.any;
244 attempt: typeof Bluebird.attempt;
245 bind: typeof Bluebird.bind;
246 cast: typeof Bluebird.cast;
247 config: typeof Bluebird.config;
248 coroutine: typeof Bluebird.coroutine;
249 defer: typeof Bluebird.defer;
250 delay: typeof Bluebird.delay;
251 each: typeof Bluebird.each;
252 filter: typeof Bluebird.filter;
253 fromCallback: typeof Bluebird.fromCallback;
254 fromNode: typeof Bluebird.fromNode;
255 is: typeof Bluebird.is;
256 join: typeof Bluebird.join;
257 longStackTraces: typeof Bluebird.longStackTraces;
258 map: typeof Bluebird.map;
259 mapSeries: typeof Bluebird.mapSeries;
260 method: typeof Bluebird.method;
261 onPossiblyUnhandledRejection: typeof Bluebird.onPossiblyUnhandledRejection;
262 promisify: typeof Bluebird.promisify;
263 promisifyAll: typeof Bluebird.promisifyAll;
264 props: typeof Bluebird.props;
265 // race: typeof Bluebird.race; // Provided by lib.es2015.d.ts
266 reduce: typeof Bluebird.reduce;
267 // reject: typeof Bluebird.reject; // Provided by lib.es2015.d.ts
268 // resolve: typeof Bluebird.resolve; // Provided by lib.es2015.d.ts
269 some: typeof Bluebird.some;
270 try: typeof Bluebird.try;
271 using: typeof Bluebird.using;
272
273 /*
274 * Copy&paste from lib.es2015.promise.d.ts, because Bluebird's typings are not in line with the standard lib.
275 *
276 * #std-lib-copy&paste-to-remove
277 *
278 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
279 */
280 all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
281 values: [
282 T1 | PromiseLike<T1>,
283 T2 | PromiseLike<T2>,
284 T3 | PromiseLike<T3>,
285 T4 | PromiseLike<T4>,
286 T5 | PromiseLike<T5>,
287 T6 | PromiseLike<T6>,
288 T7 | PromiseLike<T7>,
289 T8 | PromiseLike<T8>,
290 T9 | PromiseLike<T9>,
291 T10 | PromiseLike<T10>,
292 ],
293 ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
294 all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
295 values: [
296 T1 | PromiseLike<T1>,
297 T2 | PromiseLike<T2>,
298 T3 | PromiseLike<T3>,
299 T4 | PromiseLike<T4>,
300 T5 | PromiseLike<T5>,
301 T6 | PromiseLike<T6>,
302 T7 | PromiseLike<T7>,
303 T8 | PromiseLike<T8>,
304 T9 | PromiseLike<T9>,
305 ],
306 ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
307 all<T1, T2, T3, T4, T5, T6, T7, T8>(
308 values: [
309 T1 | PromiseLike<T1>,
310 T2 | PromiseLike<T2>,
311 T3 | PromiseLike<T3>,
312 T4 | PromiseLike<T4>,
313 T5 | PromiseLike<T5>,
314 T6 | PromiseLike<T6>,
315 T7 | PromiseLike<T7>,
316 T8 | PromiseLike<T8>,
317 ],
318 ): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
319 all<T1, T2, T3, T4, T5, T6, T7>(
320 values: [
321 T1 | PromiseLike<T1>,
322 T2 | PromiseLike<T2>,
323 T3 | PromiseLike<T3>,
324 T4 | PromiseLike<T4>,
325 T5 | PromiseLike<T5>,
326 T6 | PromiseLike<T6>,
327 T7 | PromiseLike<T7>,
328 ],
329 ): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
330 all<T1, T2, T3, T4, T5, T6>(
331 values: [
332 T1 | PromiseLike<T1>,
333 T2 | PromiseLike<T2>,
334 T3 | PromiseLike<T3>,
335 T4 | PromiseLike<T4>,
336 T5 | PromiseLike<T5>,
337 T6 | PromiseLike<T6>,
338 ],
339 ): Promise<[T1, T2, T3, T4, T5, T6]>;
340 all<T1, T2, T3, T4, T5>(
341 values: [
342 T1 | PromiseLike<T1>,
343 T2 | PromiseLike<T2>,
344 T3 | PromiseLike<T3>,
345 T4 | PromiseLike<T4>,
346 T5 | PromiseLike<T5>,
347 ],
348 ): Promise<[T1, T2, T3, T4, T5]>;
349 all<T1, T2, T3, T4>(
350 values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>],
351 ): Promise<[T1, T2, T3, T4]>;
352 all<T1, T2, T3>(
353 values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>],
354 ): Promise<[T1, T2, T3]>;
355 all<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
356 all<T>(values: Array<T | PromiseLike<T>>): Promise<T[]>;
357 race<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
358 values: [
359 T1 | PromiseLike<T1>,
360 T2 | PromiseLike<T2>,
361 T3 | PromiseLike<T3>,
362 T4 | PromiseLike<T4>,
363 T5 | PromiseLike<T5>,
364 T6 | PromiseLike<T6>,
365 T7 | PromiseLike<T7>,
366 T8 | PromiseLike<T8>,
367 T9 | PromiseLike<T9>,
368 T10 | PromiseLike<T10>,
369 ],
370 ): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
371 race<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
372 values: [
373 T1 | PromiseLike<T1>,
374 T2 | PromiseLike<T2>,
375 T3 | PromiseLike<T3>,
376 T4 | PromiseLike<T4>,
377 T5 | PromiseLike<T5>,
378 T6 | PromiseLike<T6>,
379 T7 | PromiseLike<T7>,
380 T8 | PromiseLike<T8>,
381 T9 | PromiseLike<T9>,
382 ],
383 ): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
384 race<T1, T2, T3, T4, T5, T6, T7, T8>(
385 values: [
386 T1 | PromiseLike<T1>,
387 T2 | PromiseLike<T2>,
388 T3 | PromiseLike<T3>,
389 T4 | PromiseLike<T4>,
390 T5 | PromiseLike<T5>,
391 T6 | PromiseLike<T6>,
392 T7 | PromiseLike<T7>,
393 T8 | PromiseLike<T8>,
394 ],
395 ): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
396 race<T1, T2, T3, T4, T5, T6, T7>(
397 values: [
398 T1 | PromiseLike<T1>,
399 T2 | PromiseLike<T2>,
400 T3 | PromiseLike<T3>,
401 T4 | PromiseLike<T4>,
402 T5 | PromiseLike<T5>,
403 T6 | PromiseLike<T6>,
404 T7 | PromiseLike<T7>,
405 ],
406 ): Promise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
407 race<T1, T2, T3, T4, T5, T6>(
408 values: [
409 T1 | PromiseLike<T1>,
410 T2 | PromiseLike<T2>,
411 T3 | PromiseLike<T3>,
412 T4 | PromiseLike<T4>,
413 T5 | PromiseLike<T5>,
414 T6 | PromiseLike<T6>,
415 ],
416 ): Promise<T1 | T2 | T3 | T4 | T5 | T6>;
417 race<T1, T2, T3, T4, T5>(
418 values: [
419 T1 | PromiseLike<T1>,
420 T2 | PromiseLike<T2>,
421 T3 | PromiseLike<T3>,
422 T4 | PromiseLike<T4>,
423 T5 | PromiseLike<T5>,
424 ],
425 ): Promise<T1 | T2 | T3 | T4 | T5>;
426 race<T1, T2, T3, T4>(
427 values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>],
428 ): Promise<T1 | T2 | T3 | T4>;
429 race<T1, T2, T3>(
430 values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>],
431 ): Promise<T1 | T2 | T3>;
432 race<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<T1 | T2>;
433 race<T>(values: Array<T | PromiseLike<T>>): Promise<T>;
434 reject(reason: any): Promise<never>;
435 reject<T>(reason: any): Promise<T>;
436 resolve<T>(value: T | PromiseLike<T>): Promise<T>;
437 resolve(): Promise<void>;
438 }
439
440 /*
441 * Declare the `Promise` variable. This is needed for es5 only and is a no-op for all other targets.
442 *
443 * #std-lib-copy&paste-to-remove
444 *
445 * @todo See the comment near the top of the file about code marked with #std-lib-copy&paste-to-remove
446 */
447 var Promise: PromiseConstructor;
448}
449
\No newline at end of file