diff --git a/types/angular/angular-tests.ts b/types/angular/angular-tests.ts index 795e74a058..7f5649befb 100644 --- a/types/angular/angular-tests.ts +++ b/types/angular/angular-tests.ts @@ -548,80 +548,158 @@ namespace TestPromise { const promise: angular.IPromise = null; const $q: angular.IQService = null; - const assertPromiseType = (arg: angular.IPromise) => arg; const reject = $q.reject(); // promise.then - assertPromiseType(promise.then((result) => any)); - assertPromiseType(promise.then((result) => any, (any) => any)); - assertPromiseType(promise.then((result) => any, (any) => any, (any) => any)); + // $ExpectType IPromise + promise.then(result => any); + // $ExpectType IPromise + promise.then(result => any, any => any); + // $ExpectType IPromise + promise.then(result => any, any => any, any => any); - assertPromiseType(promise.then((result) => reject)); - assertPromiseType(promise.then((result) => reject, (any) => reject)); - assertPromiseType(promise.then((result) => reject, (any) => reject, (any) => any)); + // $ExpectType IPromise + promise.then(result => reject); + // $ExpectType IPromise + promise.then(result => reject, any => reject); + // $ExpectType IPromise + promise.then(result => reject, any => reject, any => any); - assertPromiseType(promise.then((result) => result)); - assertPromiseType(promise.then((result) => tresult)); - assertPromiseType(promise.then((result) => tresultPromise)); - assertPromiseType(promise.then((result) => result, (any) => any)); - assertPromiseType(promise.then((result) => result, (any) => any, (any) => any)); - assertPromiseType(promise.then((result) => result, (any) => reject, (any) => any)); + // $ExpectType IPromise + promise.then(result => result); + // $ExpectType IPromise + promise.then(result => tresult); + // $ExpectType IPromise + promise.then(result => tresultPromise); + // $ExpectType IPromise + promise.then(result => result, any => any); + // $ExpectType IPromise + promise.then(result => result, any => Math.random()); + // $ExpectType IPromise + promise.then(result => result, any => any, any => any); + // $ExpectType IPromise + promise.then(result => result, any => Math.random(), any => any); + // $ExpectType IPromise + promise.then(result => result, any => reject, any => any); - assertPromiseType | TResult>(promise.then((result) => anyOf2(reject, result))); - assertPromiseType(promise.then((result) => anyOf3(result, tresultPromise, reject))); - assertPromiseType(promise.then( - (result) => anyOf3(reject, result, tresultPromise), - (reason) => anyOf3(reject, tresult, tresultPromise) - )); + // $ExpectType IPromise + promise.then(result => anyOf2(reject, result)); + // $ExpectType IPromise + promise.then(result => anyOf3(result, tresultPromise, reject)); + // $ExpectType IPromise + promise.then( + result => anyOf3(reject, result, tresultPromise), + reason => anyOf3(reject, tresult, tresultPromise) + ); - assertPromiseType>(promise.then((result) => tresultHttpPromise)); + // $ExpectType IPromise> + promise.then(result => tresultHttpPromise); - assertPromiseType(promise.then((result) => result, (any) => tother)); - assertPromiseType | angular.IPromise | TOther | angular.IPromise>(promise.then( - (result) => anyOf3(reject, result, totherPromise), - (reason) => anyOf3(reject, tother, tresultPromise) - )); + // $ExpectType IPromise + promise.then(result => result, any => tother); - assertPromiseType(promise.then( - (result) => anyOf3(tresultPromise, result, totherPromise) - )); + // These are broken and seemingly can't be made to work + // with the current limitations of TypeScript. - assertPromiseType(promise.then((result) => result, (any) => tother, (any) => any)); - assertPromiseType(promise.then((result) => tresultPromise, (any) => totherPromise)); - assertPromiseType(promise.then((result) => tresultPromise, (any) => totherPromise, (any) => any)); - assertPromiseType>(promise.then((result) => tresultHttpPromise, (any) => totherHttpPromise)); - assertPromiseType>(promise.then((result) => tresultHttpPromise, (any) => totherHttpPromise, (any) => any)); + // xExpectType IPromise + // promise.then(result => anyOf2(result, totherPromise)); + // xExpectType IPromise + // promise.then(result => anyOf3(reject, result, totherPromise)); + // xExpectType IPromise + // const a4 = promise.then( + // result => anyOf3(reject, result, totherPromise), + // reason => anyOf3(reject, tother, tresultPromise) + // ); - assertPromiseType(promise.then((result) => tother)); - assertPromiseType(promise.then((result) => tother, (any) => any)); - assertPromiseType(promise.then((result) => tother, (any) => any, (any) => any)); - assertPromiseType(promise.then((result) => totherPromise)); - assertPromiseType(promise.then((result) => totherPromise, (any) => any)); - assertPromiseType(promise.then((result) => totherPromise, (any) => any, (any) => any)); - assertPromiseType>(promise.then((result) => totherHttpPromise)); - assertPromiseType>(promise.then((result) => totherHttpPromise, (any) => any)); - assertPromiseType>(promise.then((result) => totherHttpPromise, (any) => any, (any) => any)); + // $ExpectType IPromise + promise.then(result => + anyOf3(tresultPromise, result, totherPromise) + ); - assertPromiseType(promise.then((result) => tresult, (any) => tother).then(ambiguous => isTResult(ambiguous) ? ambiguous.c : ambiguous.f)); + // $ExpectType IPromise + promise.then(result => result, any => tother, any => any); + // $ExpectType IPromise + promise.then(result => tresultPromise, any => totherPromise); + // $ExpectType IPromise + promise.then(result => tresultPromise, any => totherPromise, any => any); + // $ExpectType IPromise | IHttpResponse> + promise.then(result => tresultHttpPromise, any => totherHttpPromise); + // $ExpectType IPromise | IHttpResponse> + promise.then(result => tresultHttpPromise, any => totherHttpPromise, any => any); + + // $ExpectType IPromise + promise.then(result => tother); + // $ExpectType IPromise + promise.then(result => tother, any => any); + // $ExpectType IPromise + promise.then(result => tother, any => any, any => any); + // $ExpectType IPromise + promise.then(result => totherPromise); + // $ExpectType IPromise + promise.then(result => totherPromise, any => any); + // $ExpectType IPromise + promise.then(result => totherPromise, any => any, any => any); + // $ExpectType IPromise> + promise.then(result => totherHttpPromise); + // $ExpectType IPromise + promise.then(result => totherHttpPromise, any => any); + // $ExpectType IPromise + promise.then(result => totherHttpPromise, any => any, any => any); + + // $ExpectType IPromise + promise + .then(result => tresult, any => tother) + .then(ambiguous => (isTResult(ambiguous) ? ambiguous.c : ambiguous.f)); + + // promise.then + $q.reject: + + // $ExpectType IPromise + $q.resolve(true).then(result => Math.random() > 0.5 ? Math.random() : $q.reject()); + // $ExpectType IPromise + $q.resolve(true).then(result => Math.random() > 0.5 ? Math.random() : $q.reject('a')); + // $ExpectType IPromise + $q.resolve().then(() => $q.reject('a')); + // $ExpectType IPromise + $q.resolve().then(() => { + if (Math.random() > 0.5) $q.reject(); + }); // promise.catch - assertPromiseType(promise.catch((err) => err)); - assertPromiseType(promise.catch((err) => any)); - assertPromiseType(promise.catch((err) => tresult)); - assertPromiseType>(promise.catch((err) => anyOf2(tresult, reject))); - assertPromiseType(promise.catch((err) => anyOf3(tresult, tresultPromise, reject))); - assertPromiseType(promise.catch((err) => tresultPromise)); - assertPromiseType>(promise.catch((err) => tresultHttpPromise)); - assertPromiseType(promise.catch((err) => tother)); - assertPromiseType(promise.catch((err) => totherPromise)); - assertPromiseType>(promise.catch((err) => totherHttpPromise)); + // $ExpectType IPromise + promise.catch(err => err); + // $ExpectType IPromise + promise.catch(err => any); + // $ExpectType IPromise + promise.catch(err => tresult); + // $ExpectType IPromise + promise.catch(err => anyOf2(tresult, reject)); + // $ExpectType IPromise + promise.catch(err => anyOf3(tresult, tresultPromise, reject)); + // $ExpectType IPromise + promise.catch(err => tresultPromise); + // $ExpectType IPromise> + promise.catch(err => tresultHttpPromise); + // $ExpectType IPromise + promise.catch(err => tother); + // $ExpectType IPromise + promise.catch(err => totherPromise); + // $ExpectType IPromise> + promise.catch(err => totherHttpPromise); - assertPromiseType(promise.catch((err) => tother).then(ambiguous => isTResult(ambiguous) ? ambiguous.c : ambiguous.f)); + // $ExpectType IPromise + promise + .catch(err => tother) + .then( + ambiguous => (isTResult(ambiguous) ? ambiguous.c : ambiguous.f) + ); // promise.finally - assertPromiseType(promise.finally(() => any)); - assertPromiseType(promise.finally(() => tresult)); - assertPromiseType(promise.finally(() => tother)); + // $ExpectType IPromise + promise.finally(() => any); + // $ExpectType IPromise + promise.finally(() => tresult); + // $ExpectType IPromise + promise.finally(() => tother); } function test_angular_forEach() { @@ -1187,6 +1265,24 @@ function NgModelControllerTyping() { }; } +// FormController +angular.module('app').directive('formDebugChecker', () => { + return { + require: '^^form', + link: (scope, el, attrs, ctrl) => { + const form = ctrl as angular.IFormController; + el.on('click', () => { + const report = [] as string[]; + angular.forEach(form.$error, (controls, validationErrorKey) => { + const names = controls.map(control => control.$name); + report.push(`${validationErrorKey}: ${controls.length} control(s) (${names.join(', ')})`); + }); + console.log(`This form has ${report.length} error(s).\n${report.join('\n')}`); + }); + } + }; +}); + let $filter: angular.IFilterService; function testFilter() { diff --git a/types/angular/index.d.ts b/types/angular/index.d.ts index efbabaa302..32a5c74b52 100644 --- a/types/angular/index.d.ts +++ b/types/angular/index.d.ts @@ -357,9 +357,9 @@ declare namespace angular { $valid: boolean; $invalid: boolean; $submitted: boolean; - $error: any; - $name: string; - $pending: any; + $error: { [validationErrorKey: string]: Array }; + $name?: string; + $pending?: { [validationErrorKey: string]: Array }; $addControl(control: INgModelController | IFormController): void; $removeControl(control: INgModelController | IFormController): void; $setValidity(validationErrorKey: string, isValid: boolean, control: INgModelController | IFormController): void; @@ -399,8 +399,8 @@ declare namespace angular { $parsers: IModelParser[]; $formatters: IModelFormatter[]; $viewChangeListeners: IModelViewChangeListener[]; - $error: any; - $name: string; + $error: { [validationErrorKey: string]: boolean }; + $name?: string; $touched: boolean; $untouched: boolean; @@ -408,7 +408,7 @@ declare namespace angular { $validators: IModelValidators; $asyncValidators: IAsyncModelValidators; - $pending: any; + $pending?: { [validationErrorKey: string]: boolean }; $pristine: boolean; $dirty: boolean; $valid: boolean; @@ -1103,28 +1103,43 @@ declare namespace angular { interface IPromise { /** - * Regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected. - * The successCallBack may return IPromise for when a $q.reject() needs to be returned - * This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback. It also notifies via the return value of the notifyCallback method. The promise can not be resolved or rejected from the notifyCallback method. + * Regardless of when the promise was or will be resolved or rejected, then calls one of + * the success or error callbacks asynchronously as soon as the result is available. The + * callbacks are called with a single argument: the result or rejection reason. + * Additionally, the notify callback may be called zero or more times to provide a + * progress indication, before the promise is resolved or rejected. + * The `successCallBack` may return `IPromise` for when a `$q.reject()` needs to + * be returned. + * This method returns a new promise which is resolved or rejected via the return value + * of the `successCallback`, `errorCallback`. It also notifies via the return value of + * the `notifyCallback` method. The promise can not be resolved or rejected from the + * `notifyCallback` method. */ - then(successCallback: (promiseValue: T) => IPromise|TResult, errorCallback?: null, notifyCallback?: (state: any) => any): IPromise; - then(successCallback: (promiseValue: T) => IPromise|TResult2, errorCallback?: null, notifyCallback?: (state: any) => any): IPromise; - - then(successCallback: (promiseValue: T) => IPromise|TResult, errorCallback: (reason: any) => IPromise|TCatch, notifyCallback?: (state: any) => any): IPromise; - then(successCallback: (promiseValue: T) => IPromise|TResult2, errorCallback: (reason: any) => IPromise|TCatch2, notifyCallback?: (state: any) => any): IPromise; + then( + successCallback?: + | ((value: T) => IPromise | IPromise | TResult1) + | null, + errorCallback?: + | ((reason: any) => IPromise | IPromise | TResult2) + | null, + notifyCallback?: (state: any) => any + ): IPromise; /** * Shorthand for promise.then(null, errorCallback) */ - catch(onRejected: (reason: any) => IPromise|TCatch): IPromise; - catch(onRejected: (reason: any) => IPromise|TCatch2): IPromise; + catch( + onRejected?: + | ((reason: any) => IPromise | IPromise | TResult) + | null + ): IPromise; /** * Allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information. * * Because finally is a reserved word in JavaScript and reserved keywords are not supported as property names by ES3, you'll need to invoke the method like promise['finally'](callback) to make your code IE8 and Android 2.x compatible. */ - finally(finallyCallback: () => any): IPromise; + finally(finallyCallback: () => void): IPromise; } interface IDeferred {