TypeScript · Junior · Question 26 of 75
What does the ! operator do in user!.name?
- ✓Correct answer: Asserts the value isn't
null/undefined, with no runtime check - 2Logical negation of
user, like the!operator in JavaScript - 3Makes the property access safe, like optional chaining
?.
Explanation
A non-null assertion removes null | undefined from the type. If the value is null after all, you get a runtime error. Safer options are an explicit check or user?.name (optional chaining, returns undefined).