What is type narrowing?

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.

selector: 'my-component',
template: '<span *ngIf="user"> {{user.contact.email}} </span>'
class MyComponent {
user?: User;

February 08, 2022