Get in touch

Send an email to: lammers@gmail.com.
Or find me online at: Github, X

Non-null assertion operator

The exclamation mark ! operator in TypeScript can be used for non-null assertion. It tells the compiler that a variable or property will not be null or undefined at runtime. This can be useful when you know that a value will be defined, but the compiler is not able to infer it.

For example when querying the DOM for an element that we know exists:

<div id="container"></div>

// Appending the `!` operator here
const container = document.querySelector('#container')!

// TypeScript will not complain about the container being possibly `null`
container.addEventListener('click', () => console.log('Container clicked'))