0 votes
in Angular by

What is type narrowing?

1 Answer

0 votes
by
The expression used in an ngIf directive is used to narrow type unions in the Angular template compiler similar to if expression in typescript. So *ngIf allows the typeScript compiler to infer that the data used in the binding expression will never be undefined.

@Component({

  selector: 'my-component',

  template: '<span *ngIf="user"> {{user.contact.email}} </span>'

})

class MyComponent {

  user?: User;

}
...