InterviewHubPRO
Section Overview
Q95

Question 95 of 283

Part 4: JIT vs AOT Compilation, Metadata & TypeScript (76 - 100)

34%
Angular & Signals (283 Qs)•Part 4: JIT vs AOT Compilation, Metadata & TypeScript (76 - 100)

What is Non null type assertion operator?

You can use the non-null type assertion operator to suppress the Object is possibly 'undefined' error. In the following example, the user and contact properties are always set together, implying that contact is always non-null if user is non-null. The error is suppressed in the example by using contact!.email.

javascript
@Component({
selector: 'my-component',
template: '<span *ngIf="user"> {{user.name}} contacted through {{contact!.email}} </span>'
})
class MyComponent {
user?: User;
contact?: Contact;

setData(user: User, contact: Contact) {
 this.user = user;
 this.contact = contact;
}
}