Search⌘ K

Non-Null Assertion Operator

Explore the non-null assertion operator in TypeScript and understand when and how to use it responsibly. This lesson helps you balance removing strict null errors while avoiding potential runtime issues, especially in classes and large codebases with strict null checks enabled.

Overview

So far, we haven’t talked about how to fix the kind of errors that are triggered by strict null checks. The reason I haven’t mentioned a non-null assertion operator is that you shouldn’t be using it unless you have a very good reason to do so.

Non-null assertion operators are simply a way of telling the compiler, trust me, I’m absolutely sure that this optional value is in fact never empty. Syntactically, you can express this by postfixing the ...