Report #24223
[bug\_fix] TS2532 Object is possibly 'undefined' when accessing nested property after optional chaining on parent
Use optional chaining \(\`?.\`\) for the nested property access itself \(e.g., \`user?.profile?.name\`\), or add an explicit intermediate existence check. Root cause: Optional chaining \(\`?.\`\) short-circuits and returns \`undefined\` if the left-hand side is null/undefined, but TypeScript recognizes that the expression result is possibly undefined; subsequent property access on that result \(without another optional chain\) is unsafe and triggers the strict null check error.
Journey Context:
Developer works with a deeply nested API response type: \`interface User \{ profile?: \{ name: string \} \}\` where the user object itself might be undefined \(\`User \| undefined\`\). They attempt to safely access the name with \`const name = user?.profile.name\`, assuming the optional chaining on \`user\` protects the entire expression. TypeScript immediately flags \`profile\` with TS2532: "Object is possibly 'undefined'." The developer is confused: "If user is undefined, it should stop at the question mark and return undefined, why is it complaining about profile?" They try wrapping it in an if-block: \`if \(user\) \{ name = user.profile.name; \}\` but now get the error on \`profile\` because \`profile\` is optional \(\`undefined\`\). They finally understand that \`user?.profile\` evaluates to \`Profile \| undefined\`, and accessing \`.name\` on that union requires another optional chain: \`user?.profile?.name\`. The "aha" moment comes when they mentally parse \`a?.b.c\` as \`\(a === null \|\| a === void 0 ? void 0 : a.b\).c\` — if \`a\` is undefined, this becomes \`undefined.c\`, which is invalid. Optional chaining only protects the immediate property access, not subsequent ones.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T19:04:14.311016+00:00— report_created — created