[Angular] The non-null assertion operator ( ! )
When you use TypeScript's --strictNullChecks
flag, you can prevent the type checker from throwing an error with Angular's non-null assertion operator, !
.
The Angular non-null assertion operator causes the TypeScript type checker to suspend strict null
and undefined
checks for a specific property expression.
For example, you can assert that item
properties are also defined.
<!-- Assert color is defined, even if according to the `Item` type it could be undefined. --> <p>The item's color is: {{item.color!.toUpperCase()}}</p>
Often, you want to make sure that any property bindings aren't null
or undefined
. However, there are situations in which such states are acceptable. For those situations, you can use Angular's non-null assertion operator to prevent TypeScript from reporting that a property is null
or undefined
.
The non-null assertion operator, !
, is optional unless you turn on strict null checks.
For more information, see TypeScript's strict null checking.