Currently I stumbled upon the ContentChildren decorator of Angular. In the first code example the following syntax is used:
import {AfterContentInit, ContentChildren, Directive, QueryList} from '@angular/core';
@Directive({selector: 'child-directive'})
class ChildDirective {
}
@Directive({selector: 'someDir'})
class SomeDir implements AfterContentInit {
@ContentChildren(ChildDirective) contentChildren !: QueryList<ChildDirective>; // this line is relevant
ngAfterContentInit() {
// contentChildren is set
}
}
Note the exclamation mark followed by a colon right after the @ContentChildren(ChildDirective) contentChildren
variable definition. In this StackOverflow thread I discovered that this syntax can be used as a "non-null assertion operator" when accessing a variable or object property.
My question is now whether the exclamation mark before a type definition has exactly the same meaning like in a normal context. Does it simply say the TypeScript compiler: "Okay, don't worry about null
or undefined", or does this syntax have another specific meaning in this context?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…