Angular IPromise interop with platform Promise.

Angular promises support `.then()` chaining of functions that return
arbitrary `then()`able values.
    https://docs.angularjs.org/api/ng/service/$q#the-promise-api

This change updates the definition of `ng.IPromise.then()` to match that
by overloading the function to handle `PromiseLike` values.
This commit is contained in:
Martin Probst 2019-02-06 14:08:06 +01:00
parent ab0806bfb5
commit 970fc9d484
3 changed files with 25 additions and 2 deletions

View File

@ -294,6 +294,14 @@ foo.then((x) => {
x.toFixed();
});
namespace TestPromiseInterop {
declare const promiseInterop: ng.IPromise<number>;
const ngStringPromise: ng.IPromise<string> =
promiseInterop.then((num) => Promise.resolve(String(num)));
const caughtStringPromise: ng.IPromise<string|number> =
promiseInterop.catch((reason) => Promise.resolve('oh noes'));
}
// $q signature tests
namespace TestQ {
interface AbcObject {

View File

@ -1197,6 +1197,15 @@ declare namespace angular {
* the `notifyCallback` method. The promise can not be resolved or rejected from the
* `notifyCallback` method.
*/
then<TResult1 = T, TResult2 = never>(
successCallback?:
| ((value: T) => PromiseLike<never> | PromiseLike<TResult1> | TResult1)
| null,
errorCallback?:
| ((reason: any) => PromiseLike<never> | PromiseLike<TResult2> | TResult2)
| null,
notifyCallback?: (state: any) => any
): IPromise<TResult1 | TResult2>;
then<TResult1 = T, TResult2 = never>(
successCallback?:
| ((value: T) => IPromise<never> | IPromise<TResult1> | TResult1)
@ -1210,6 +1219,11 @@ declare namespace angular {
/**
* Shorthand for promise.then(null, errorCallback)
*/
catch<TResult = never>(
onRejected?:
| ((reason: any) => PromiseLike<never> | PromiseLike<TResult> | TResult)
| null
): IPromise<T | TResult>;
catch<TResult = never>(
onRejected?:
| ((reason: any) => IPromise<never> | IPromise<TResult> | TResult)

View File

@ -11,7 +11,8 @@
"lib": [
"es5",
"dom",
"es2015.iterable"
"es2015.iterable",
"es2015.promise"
],
"noImplicitAny": false,
"noImplicitThis": false,
@ -25,4 +26,4 @@
"noEmit": true,
"forceConsistentCasingInFileNames": true
}
}
}