TypeScript Bug Hunt Medium +20 XP
Null Check Missing
This crashes when user is null.
TypeScriptFind the bug
1
function getNameLength(user: { name: string } | null) {2
return user.name.length;3
} The bug is on line 2
Select the correct fix
Show answer and explanation
Answer: Add null check: if (!user) return 0
When user could be null, you must check for null before accessing properties.
Hint: What happens when user is null?
What this challenge tests
Bug Hunt in TypeScript: null-safety, narrowing.