diff --git a/bluebird-global/bluebird-global-tests.ts b/bluebird-global/bluebird-global-tests.ts new file mode 100644 index 0000000000..9087e57d08 --- /dev/null +++ b/bluebird-global/bluebird-global-tests.ts @@ -0,0 +1,31 @@ +function testSomeStaticMethods() { + Promise.config({}); + Promise.delay(100).then(() => {}); + Promise.all([]).finally(() => {}); +} + +function testFunctionReturningPromise() { + function functionReturningPromise(): Promise { + return new Promise((resolve, reject, onCancel) => { + + if (onCancel) { + onCancel(() => { + console.log("onCancel cleanup"); + }); + } + + resolve("lorem ipsum"); + }) + .then((value) => { + return value + " dolor"; + }); + } + + functionReturningPromise() + .then((value) => { + console.log("then callback: " + value); + }) + .finally(() => { + console.log("finally callback"); + }); +} diff --git a/bluebird-global/index.d.ts b/bluebird-global/index.d.ts new file mode 100644 index 0000000000..81a352ce5a --- /dev/null +++ b/bluebird-global/index.d.ts @@ -0,0 +1,82 @@ +// Type definitions for bluebird 3.0 +// Project: https://github.com/petkaantonov/bluebird +// Definitions by: d-ph +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/* + * 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. + * + * 2. How to use it? + * + * Add `bluebird-global` to the `types` array in your `tsconfig.json` like this: + * + * { + * "compilerOptions": { + * "types": [ + * "bluebird-global" + * ], + * } + * } + * + * 3. Why so much effort? + * + * If a promise-polyfilling library wants to play nicely with TypeScript, it needs to augment + * the Promise and PromiseConstructor interfaces defined in the standard ts library. + * For various reasons this couldn't be done in The `bluebird` typings. + * + */ + +import * as Bluebird from "bluebird"; + +declare global { + /* + * Patch all instance method + */ + interface Promise extends Bluebird {} + + /* + * Patch all static methods and the constructor + */ + interface PromiseConstructor { + new (callback: (resolve: (thenableOrResult?: T | Bluebird.Thenable) => void, reject: (error?: any) => void, onCancel?: (callback: () => void) => void) => void): Promise; + + all: typeof Bluebird.all; + 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; + reduce: typeof Bluebird.reduce; + reject: typeof Bluebird.reject; + resolve: typeof Bluebird.resolve; + some: typeof Bluebird.some; + try: typeof Bluebird.try; + using: typeof Bluebird.using; + } + + /* + * Declare the `Promise` variable. This is needed for es5 only and is a no-op for all other targets. + */ + var Promise: PromiseConstructor; +} diff --git a/bluebird-global/tsconfig.json b/bluebird-global/tsconfig.json new file mode 100644 index 0000000000..078363a850 --- /dev/null +++ b/bluebird-global/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "bluebird-global-tests.ts" + ] +} diff --git a/bluebird-global/tslint.json b/bluebird-global/tslint.json new file mode 100644 index 0000000000..9b7feabed9 --- /dev/null +++ b/bluebird-global/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "../tslint.json", + "rules": { + "no-empty-interface": false + } +} diff --git a/bluebird/index.d.ts b/bluebird/index.d.ts index 3087abb2e5..2faa74e285 100644 --- a/bluebird/index.d.ts +++ b/bluebird/index.d.ts @@ -7,6 +7,9 @@ * The code following this comment originates from: * https://github.com/types/npm-bluebird * + * Note for browser users: use bluebird-global typings instead of this one + * if you want to use Bluebird via the global Promise symbol. + * * Licensed under: * The MIT License (MIT) * @@ -777,4 +780,4 @@ declare namespace Bluebird { export function setScheduler(scheduler: (callback: (...args: any[]) => void) => void): void; } -export = Bluebird; \ No newline at end of file +export = Bluebird;