From ac85b43a1cf7e4fe41c7ef48064543c97212e70c Mon Sep 17 00:00:00 2001 From: use-strict Date: Tue, 28 Jul 2015 14:05:20 +0300 Subject: [PATCH 1/2] Type safety for $watch and $watchCollection Added generic constraint to watched values, replacing "any". Using "any" looses type safety. The type should be inferred from the return type of the watching function, or given explicitly when using a watch expression. --- angularjs/angular.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/angularjs/angular.d.ts b/angularjs/angular.d.ts index 746fbb0e0d..c8caec802d 100755 --- a/angularjs/angular.d.ts +++ b/angularjs/angular.d.ts @@ -617,12 +617,12 @@ declare module angular { $on(name: string, listener: (event: IAngularEvent, ...args: any[]) => any): Function; $watch(watchExpression: string, listener?: string, objectEquality?: boolean): Function; - $watch(watchExpression: string, listener?: (newValue: any, oldValue: any, scope: IScope) => any, objectEquality?: boolean): Function; + $watch(watchExpression: string, listener?: (newValue: T, oldValue: T, scope: IScope) => any, objectEquality?: boolean): Function; $watch(watchExpression: (scope: IScope) => any, listener?: string, objectEquality?: boolean): Function; - $watch(watchExpression: (scope: IScope) => any, listener?: (newValue: any, oldValue: any, scope: IScope) => any, objectEquality?: boolean): Function; + $watch(watchExpression: (scope: IScope) => T, listener?: (newValue: T, oldValue: T, scope: IScope) => any, objectEquality?: boolean): Function; - $watchCollection(watchExpression: string, listener: (newValue: any, oldValue: any, scope: IScope) => any): Function; - $watchCollection(watchExpression: (scope: IScope) => any, listener: (newValue: any, oldValue: any, scope: IScope) => any): Function; + $watchCollection(watchExpression: string, listener: (newValue: T, oldValue: T, scope: IScope) => any): Function; + $watchCollection(watchExpression: (scope: IScope) => T, listener: (newValue: T, oldValue: T, scope: IScope) => any): Function; $watchGroup(watchExpressions: any[], listener: (newValue: any, oldValue: any, scope: IScope) => any): Function; $watchGroup(watchExpressions: { (scope: IScope): any }[], listener: (newValue: any, oldValue: any, scope: IScope) => any): Function; From 35527887e20f7d865aa884ade5af888fd9e88f25 Mon Sep 17 00:00:00 2001 From: use-strict Date: Tue, 28 Jul 2015 14:14:29 +0300 Subject: [PATCH 2/2] Fixed missing generic constraint --- angularjs/angular.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/angularjs/angular.d.ts b/angularjs/angular.d.ts index c8caec802d..4c4e71190c 100755 --- a/angularjs/angular.d.ts +++ b/angularjs/angular.d.ts @@ -622,7 +622,7 @@ declare module angular { $watch(watchExpression: (scope: IScope) => T, listener?: (newValue: T, oldValue: T, scope: IScope) => any, objectEquality?: boolean): Function; $watchCollection(watchExpression: string, listener: (newValue: T, oldValue: T, scope: IScope) => any): Function; - $watchCollection(watchExpression: (scope: IScope) => T, listener: (newValue: T, oldValue: T, scope: IScope) => any): Function; + $watchCollection(watchExpression: (scope: IScope) => T, listener: (newValue: T, oldValue: T, scope: IScope) => any): Function; $watchGroup(watchExpressions: any[], listener: (newValue: any, oldValue: any, scope: IScope) => any): Function; $watchGroup(watchExpressions: { (scope: IScope): any }[], listener: (newValue: any, oldValue: any, scope: IScope) => any): Function;