What is the !. operator?

I've been working on filtering out selectable options recently, and came across the following interesting bit of code:

option!.children.trim().toLowerCase().indexOf(input.trim().toLowerCase()) >= 0

Notice the !. after option? I did, and I thought what does it do? And since it was quite tricky to find an answer online I figured I'd quickly explain it here so I can easily find it next time.

!. is the non-null assertion operator. It basically tells the compiler that this expression cannot be null or undefined here, so don't complain about it even being a possibility. So in the above example, there is no possible way children can be null/undefined and so by using !. we avoid the compiler throwing a type error.

Further reading can be found here.

All rights reserved © Joshua Bosman 2025