Q15: What is the Non-Null Assertion Operator (`!`) and when should it be avoided?
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 removesnullandundefinedfrom a type at compile time. - It generates no runtime JavaScript protective code—use it with caution.