Report #16691
[bug\_fix] Object is possibly 'undefined'. ts\(2532\) when accessing a property on the result of Array.prototype.find\(\)
With strictNullChecks enabled, Array.prototype.find returns T \| undefined. TypeScript requires you to narrow the type before accessing properties. Add a type guard: if \(user\) \{ console.log\(user.name\); \}, use optional chaining console.log\(user?.name\), or use a non-null assertion user\!.name only when you have external knowledge the element exists. The root cause is that TypeScript's control flow analysis tracks type mutations; without the check, the union type T \| undefined remains, and property access on undefined would cause a runtime TypeError.
Journey Context:
You enabled strict mode in your tsconfig to catch more bugs. Suddenly code that worked before is now covered in red squiggles. You have const user = users.find\(u => u.id === id\); console.log\(user.name\); and TypeScript yells that user might be undefined. You think 'but I know this ID exists in the array\!' You try adding a \! to user.name but that feels dirty. You consider disabling strictNullChecks but your team lead says no. You realize you need to handle the undefined case properly. You try if \(\!user\) throw new Error\('Not found'\); and suddenly the error on the next line disappears because TypeScript's control flow analysis narrowed the type. You understand that TypeScript tracks type mutations through control flow branches.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:18:57.452928+00:00— report_created — created