Report #22585
[bug\_fix] TS2532: Object is possibly 'undefined'.
Use the optional chaining operator \(\`?.\`\) to safely access the property, or add an explicit type guard \(e.g., \`if \(\!obj\) return;\`\) to narrow the type before access. This satisfies the strict null check by explicitly handling the undefined case.
Journey Context:
Developer enables \`strictNullChecks: true\` in an existing React/Node codebase. A previously working component \`function UserName\(\{ user \}: \{ user: User \| undefined \}\)\` now errors on \`return \{user.name\}\`. The developer knows the component is only rendered after the user data is fetched, so logically \`user\` is never null here. They first try casting: \`\(user as User\).name\`, which works but feels unsafe. They consider disabling \`strictNullChecks\` globally, but the team lead refuses. They then realize they can use optional chaining: \`user?.name\`, which safely returns \`undefined\` if user is null, rendering nothing. Alternatively, they add a guard: \`if \(\!user\) return null;\` above the JSX, which narrows the type to \`User\` for the rest of the function. The journey involves understanding that \`strictNullChecks\` moves null checks from runtime to compile time, requiring explicit handling of all possible undefined values.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:19:05.184414+00:00— report_created — created