Add bluebird-global typings for bluebird.js. (#13541)

* Add bluebird-global typings for bluebird.js.

`bluebird-global` uses `bluebird` typings in order to expose its functions
on the global Promise symbol. This is generally how devs use bluebird.js
in the browser.

* Sort methods alphabetically in `bluebird-global` and apply some code style fixes.

* Apply code style fixes in `bluebird-global`.

* Add `noImplicitThis: true` to `bluebird-global/tsconfig.ts`.
This commit is contained in:
d-ph
2016-12-29 15:55:04 +00:00
committed by Andy
parent 6772d3f933
commit cd33100747
5 changed files with 143 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
function testSomeStaticMethods() {
Promise.config({});
Promise.delay(100).then(() => {});
Promise.all([]).finally(() => {});
}
function testFunctionReturningPromise() {
function functionReturningPromise(): Promise<string> {
return new Promise<string>((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");
});
}

82
bluebird-global/index.d.ts vendored Normal file
View File

@@ -0,0 +1,82 @@
// Type definitions for bluebird 3.0
// Project: https://github.com/petkaantonov/bluebird
// Definitions by: d-ph <https://github.com/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<T> 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<T> extends Bluebird<T> {}
/*
* Patch all static methods and the constructor
*/
interface PromiseConstructor {
new <T>(callback: (resolve: (thenableOrResult?: T | Bluebird.Thenable<T>) => void, reject: (error?: any) => void, onCancel?: (callback: () => void) => void) => void): Promise<T>;
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;
}

View File

@@ -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"
]
}

View File

@@ -0,0 +1,6 @@
{
"extends": "../tslint.json",
"rules": {
"no-empty-interface": false
}
}

5
bluebird/index.d.ts vendored
View File

@@ -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;
export = Bluebird;