As I delved into the Angular 2 source code, my curiosity was piqued by NgForOfContext<T>
, located in the official repository here. What caught my attention was a particular syntax that seemed unfamiliar to me in both plain Javascript and Typescript.
The snippet of interest is as follows:
private _applyChanges(changes: IterableChanges<T>) {
const insertTuples: RecordViewTuple<T>[] = [];
changes.forEachOperation(
(item: IterableChangeRecord<any>, adjustedPreviousIndex: number, currentIndex: number) => {
if (item.previousIndex == null) {
const view = this._viewContainer.createEmbeddedView(
this._template, new NgForOfContext<T>(null !, this.ngForOf, -1, -1), currentIndex);
const tuple = new RecordViewTuple<T>(item, view);
insertTuples.push(tuple);
} else if (currentIndex == null) {
this._viewContainer.remove(adjustedPreviousIndex);
} else {
const view = this._viewContainer.get(adjustedPreviousIndex) !;
this._viewContainer.move(view, currentIndex);
const tuple = new RecordViewTuple(item, <EmbeddedViewRef<NgForOfContext<T>>>view);
insertTuples.push(tuple);
}
});
What intrigued me the most was the use of null !
in this line:
this._template, new NgForOfContext<T>(null !, this.ngForOf, -1, -1), currentIndex);
The first argument being null, followed by a logical not (!). Upon further inspection, I realized that null
is indeed a primitive type in JavaScript (and TS), making the null !
puzzling. It would seem more plausible if it were !null
(not null), which would evaluate to true
.
To confirm my doubts, I attempted to compile the code but encountered an unexpected error - Unexpected ). This left me perplexed as Angular functions flawlessly with such syntax. Clearly, there's something I'm missing here. Can someone help unravel this enigmatic mystery?