From f6a902f8ce35e613908ecc06c6809ff2f28d45aa Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Thu, 28 Jun 2018 12:27:49 +0200 Subject: [PATCH] Improve module.filter() by allowing `$stateful` to be defined. --- types/angular/angular-tests.ts | 15 +++++++++++---- types/angular/index.d.ts | 13 +++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/types/angular/angular-tests.ts b/types/angular/angular-tests.ts index 1dff1a0451..3e67c88e67 100644 --- a/types/angular/angular-tests.ts +++ b/types/angular/angular-tests.ts @@ -214,11 +214,18 @@ mod.factory({ name1(foo: any) {}, name2: ['foo', (foo: any) => {}] }); -mod.filter('name', ($scope: ng.IScope) => {}); -mod.filter('name', ['$scope', ($scope: ng.IScope) => {}]); +mod.filter('name', ($scope: ng.IScope) => () => {}); +mod.filter('name', ['$scope', ($scope: ng.IScope) => () => {}]); mod.filter({ - name1(foo: any) {}, - name2: ['foo', (foo: any) => {}] + name1(foo: any) { return () => {}; }, + name2: ['foo', (foo: any) => () => {}], +}); +const customStatefulFilter: ng.FilterFunction<(s: string) => number> = (s) => 1; +mod.filter('name', () => customStatefulFilter); +mod.filter('name', ['$scope', () => customStatefulFilter]); +mod.filter({ + name1: () => customStatefulFilter, + name2: ['foo', () => customStatefulFilter], }); mod.provider('name', ($scope: ng.IScope) => ({ $get: () => { } })); mod.provider('name', TestProvider); diff --git a/types/angular/index.d.ts b/types/angular/index.d.ts index 6b82a2bcb4..50d28a2dbb 100644 --- a/types/angular/index.d.ts +++ b/types/angular/index.d.ts @@ -250,8 +250,8 @@ declare namespace angular { */ factory(name: string, $getFn: Injectable): IModule; factory(object: {[name: string]: Injectable}): IModule; - filter(name: string, filterFactoryFunction: Injectable): IModule; - filter(object: {[name: string]: Injectable}): IModule; + filter(name: string, filterFactoryFunction: Injectable>): IModule; + filter(object: {[name: string]: Injectable>}): IModule; provider(name: string, serviceProviderFactory: IServiceProviderFactory): IModule; provider(name: string, serviceProviderConstructor: IServiceProviderClass): IModule; provider(name: string, inlineAnnotatedConstructor: any[]): IModule; @@ -2237,4 +2237,13 @@ declare namespace angular { interface IHttpParamSerializer { (obj: Object): string; } + + type FilterFunction = { + /** + * By default, filters are only run once the input value changes. By marking the filter as `$stateful`, the filter will be run on every `$digest` to update the output. **This is strongly discouraged.** + * See https://docs.angularjs.org/guide/filter#stateful-filters + */ + $stateful?: boolean + } & Filter; + type FilterFactory = (...I: any[]) => FilterFunction; }