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.
1 2 3 4 5 6 7 | @Component({ selector: 'my-component', template: '<span *ngIf="user"> {{user.contact.email}} </span>' }) class MyComponent { user?: User; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.