// Type definitions for bluebird 3.5 // Project: https://github.com/kaatt/bluebird-global // Definitions by: 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 = new Promise(() => { 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 = Bluebird.resolve( * new Promise(() => { 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 instead of Promise. This is relevant in the * following case: * * function createDelayedPromise(): Promise { * return Promise.delay(250); * } * * Since Promise.delay() returns a Bluebird and the function is typed to return a Promise, * an implicit cast is performed from Bluebird to Promise. 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 { * return > 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 `