0 votes
in Angular by

What is Non null type assertion operator?

1 Answer

0 votes
by
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.

@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;

  }

}

Related questions

0 votes
asked May 5 in JAVA by sharadyadav1986
0 votes
asked Jan 7, 2021 in Other by SakshiSharma
...