From 514a27de01ad899feccc2898e3c2b8768fa90f02 Mon Sep 17 00:00:00 2001 From: Caleb St-Denis Date: Wed, 3 May 2017 15:02:48 -0400 Subject: [PATCH] angular - Separate interfaces for each lifecycle hook (#16299) * Separate interfaces for each lifecycle hook Having one required hook per interface leads to greater type safety than having a single IController interface with all properties optional. Namely, the IController interface will not help you if you misspell a hook name. (A slight improvement, admittedly, but an improvement nonetheless.) This is closer to how the typings are done for lifecycle hooks in ng2. * Use `dtslint` * Fix trailing whitespace --- types/angular/index.d.ts | 73 ++++++++++++++++++++++++++++++++++++++- types/angular/tslint.json | 2 +- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/types/angular/index.d.ts b/types/angular/index.d.ts index 1d59854d9d..c895c4f7ee 100644 --- a/types/angular/index.d.ts +++ b/types/angular/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for Angular JS 1.6 // Project: http://angularjs.org -// Definitions by: Diego Vilar , Georgii Dolzhykov +// Definitions by: Diego Vilar , Georgii Dolzhykov , Caleb St-Denis // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -1847,6 +1847,77 @@ declare namespace angular { $postLink?(): void; } + /** + * Interface for the $onInit lifecycle hook + * https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks + */ + interface IOnInit { + /** + * Called on each controller after all the controllers on an element have been constructed and had their bindings + * initialized (and before the pre & post linking functions for the directives on this element). This is a good + * place to put initialization code for your controller. + */ + $onInit(): void; + } + + /** + * Interface for the $doCheck lifecycle hook + * https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks + */ + interface IDoCheck { + /** + * Called on each turn of the digest cycle. Provides an opportunity to detect and act on changes. + * Any actions that you wish to take in response to the changes that you detect must be invoked from this hook; + * implementing this has no effect on when `$onChanges` is called. For example, this hook could be useful if you wish + * to perform a deep equality check, or to check a `Dat`e object, changes to which would not be detected by Angular's + * change detector and thus not trigger `$onChanges`. This hook is invoked with no arguments; if detecting changes, + * you must store the previous value(s) for comparison to the current values. + */ + $doCheck(): void; + } + + /** + * Interface for the $onChanges lifecycle hook + * https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks + */ + interface IOnChanges { + /** + * Called whenever one-way bindings are updated. The onChangesObj is a hash whose keys are the names of the bound + * properties that have changed, and the values are an {@link IChangesObject} object of the form + * { currentValue, previousValue, isFirstChange() }. Use this hook to trigger updates within a component such as + * cloning the bound value to prevent accidental mutation of the outer value. + */ + $onChanges(onChangesObj: IOnChangesObject): void; + } + + /** + * Interface for the $onDestroy lifecycle hook + * https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks + */ + interface IOnDestroy { + /** + * Called on a controller when its containing scope is destroyed. Use this hook for releasing external resources, + * watches and event handlers. + */ + $onDestroy(): void; + } + + /** + * Interface for the $postLink lifecycle hook + * https://docs.angularjs.org/api/ng/service/$compile#life-cycle-hooks + */ + interface IPostLink { + /** + * Called after this controller's element and its children have been linked. Similar to the post-link function this + * hook can be used to set up DOM event handlers and do direct DOM manipulation. Note that child elements that contain + * templateUrl directives will not have been compiled and linked since they are waiting for their template to load + * asynchronously and their own compilation and linking has been suspended until that occurs. This hook can be considered + * analogous to the ngAfterViewInit and ngAfterContentInit hooks in Angular 2. Since the compilation process is rather + * different in Angular 1 there is no direct mapping and care should be taken when upgrading. + */ + $postLink(): void; + } + interface IOnChangesObject { [property: string]: IChangesObject; } diff --git a/types/angular/tslint.json b/types/angular/tslint.json index 3ace427c86..50c4474b7c 100644 --- a/types/angular/tslint.json +++ b/types/angular/tslint.json @@ -17,4 +17,4 @@ "unified-signatures": false, "void-return": false } -} +} \ No newline at end of file