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