TypeScript · Narrowing and unions

Non-null assertion

Tells the compiler a value is not null. It performs no runtime check, so use it only when the fact is guaranteed.

Syntax
value!

Example

Example
const input = document.querySelector("input");
console.log(input!.tagName);

More in this group

Assertion function Discriminated union Exhaustive switch in operator instanceof guard Type assertion Type predicate typeof guard

Learn this in the course

Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.

Open the course