Report #74600
[bug\_fix] TS2532: Object is possibly 'undefined' when accessing properties after Array.prototype.find\(\)
Use a type guard before accessing the property: \`if \(\!user\) throw new Error\('Not found'\);\`, or use optional chaining \`user?.name\`, or apply a non-null assertion \`user\!.name\` only if certain the element exists.
Journey Context:
Developer writes \`const user = users.find\(u => u.id === id\); console.log\(user.name\);\` with \`strict: true\` enabled. TypeScript immediately flags \`user.name\` with "Object is possibly 'undefined'". The developer is confused because they "know" the ID exists in the array. They try disabling \`strictNullChecks\` in \`tsconfig.json\` and the error disappears, but they don't want to lose safety. They try casting \`\(user as User\).name\` which works but feels hacky. After reading the handbook, they realize that \`Array.prototype.find\` explicitly returns \`T \| undefined\` according to the standard library definitions, and \`strictNullChecks\` forces them to handle the undefined case. The fix works because the type guard \`if \(\!user\)\` narrows the type from \`User \| undefined\` to \`User\` within the block, satisfying the compiler that the value is defined before property access.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T07:48:55.545769+00:00— report_created — created