Q15: What is the Non-Null Assertion Operator (`!`) and when should it be avoided?

Difficulty:EasyTopic:Operators

The non-null assertion operator (postfix !) tells the TypeScript compiler to assert that an expression is neither null nor undefined, suppressing strict null checking errors.

It does not emit any runtime protective checks. If the value is null or undefined at runtime, your code will throw an unhandled exception.

Key Takeaways
  • The ! operator removes null and undefined from a type at compile time.
  • It generates no runtime JavaScript protective code—use it with caution.