Report #17374
[bug\_fix] Object is possibly 'null'. TS2531
Use a type guard to narrow the type: \`if \(obj \!== null\) \{ /\* use obj \*/ \}\`. Alternatively, use the optional chaining operator \(\`obj?.property\`\) to safely access properties, or the nullish coalescing operator \(\`obj ?? defaultValue\`\) for defaults. As a last resort, use a non-null assertion \(\`obj\!.property\`\) if you have external knowledge that the value is never null at that point, though this bypasses type safety.
Journey Context:
Developer enables \`strict: true\` \(which includes \`strictNullChecks\`\) on an existing TypeScript codebase. Suddenly, hundreds of errors appear. One common pattern is \`const user = users.find\(u => u.id === id\); console.log\(user.name\);\`. TypeScript now flags \`user\` as possibly 'undefined' because \`Array.find\` returns \`T \| undefined\`. Developer initially tries to silence the error by changing it to \`console.log\(\(user as any\).name\)\`, defeating the purpose of TypeScript. They then learn about type narrowing. They rewrite the code: \`const user = users.find\(u => u.id === id\); if \(\!user\) \{ throw new Error\('User not found'\); \} console.log\(user.name\);\`. After the check, TypeScript narrows \`user\` to the non-undefined type. Developer also discovers optional chaining \`user?.name\` which safely returns \`undefined\` instead of crashing, useful for optional UI rendering. They refactor the codebase to use these patterns, resulting in safer code that handles edge cases explicitly rather than crashing at runtime.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:15:42.844523+00:00— report_created — created