Article
Non-null Assertion Operator in TypeScript
What the postfix `!` means in TypeScript and when it helps the compiler trust a value.
Have you seen something like node!.parent and wondered what the postfix ! is doing in TypeScript code?
That is the non-null assertion operator, introduced in TypeScript 2.0. As the documentation notes:
A new
!post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact.
Now let us look at a simple use case. Suppose we have document.getElementById('post'); TypeScript cannot determine whether the element will be present in the DOM. In cases like that, we can tell the type checker that the element is present by using the postfix !.
let postEl: HTMLElement = document.getElementById('post');
const post = new Blog(postEl, {
mood: '😊',
});
That will produce an error because document.getElementById() returns HTMLElement | null.
By changing the first line like this:
const postEl: HTMLElement = document.getElementById('post')!;
the error goes away and the code compiles.
The important detail is that the ! only affects the type checker. It is removed from the emitted JavaScript.
Happy coding.
Related articles
Keep reading
Aug 3, 2026
TypeScript Union Types for Payment Results
A practical introduction to TypeScript union types with a type-safe checkout result.
Mar 18, 2026
Keep Your Types Strong at the Boundaries
A reminder that TypeScript pays off most when it protects inputs, outputs, and state transitions instead of every internal detail.
May 12, 2020
Using Partials in TypeScript
How `Partial<T>` helps with type-safe updates and constructor-style object initialization.