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
This commit is contained in:
Caleb St-Denis
2017-05-03 15:02:48 -04:00
committed by Mohamed Hegazy
parent f782f81c7a
commit 514a27de01
2 changed files with 73 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
// Type definitions for Angular JS 1.6
// Project: http://angularjs.org
// Definitions by: Diego Vilar <http://github.com/diegovilar>, Georgii Dolzhykov <http://github.com/thorn0>
// Definitions by: Diego Vilar <http://github.com/diegovilar>, Georgii Dolzhykov <http://github.com/thorn0>, Caleb St-Denis <https://github.com/calebstdenis>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="jquery" />
@@ -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<any>;
}

View File

@@ -17,4 +17,4 @@
"unified-signatures": false,
"void-return": false
}
}
}