While reading Angular 2 source code, I came across NgForOfContext<T>
which sources can be found in the official repository here and, below here, the part which brought interest.
In particular the following syntax is something I have never seen in neither plain Javascript nor in Typescript:
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);
}
});
I am talking about the null !
in line:
this._template, new NgForOfContext<T>(null !, this.ngForOf, -1, -1), currentIndex);
The first argument is null, followed by a logical not (!). In my attampt to understand this code, I have double checked and as far as I know, null
is a primitive Javascript (and TS) type and it makes no sense to have a null !
. Which moreover would make more syntactical sense if it were !null
(not null) that returns true
To make sure I was wrong, I tried to compile it, but to my surprise, it fails. It complains about Unexpected )
Now, obviously Angular works. We all know that. Therefore I must be missing something. Pray, resolve this killer mystery...