Cause 1 of 6
The property is optional, or came from an optional chain.
Guard before use: if (!user.profile) return;. After that check TypeScript narrows the type for the rest of the block, so no further checks or assertions are needed.
TypeScript · 6 common causes
Updated
Under strictNullChecks the value's type includes undefined, and you used it without ruling that out.
src/report.ts:11:3 - error TS2532: Object is possibly 'undefined'.
11 user.profile.email;
~~~~~~~~~~~~
Found 1 error in src/report.ts:11Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is TS2532: Object is possibly 'undefined'.
Cause 1 of 6
Guard before use: if (!user.profile) return;. After that check TypeScript narrows the type for the rest of the block, so no further checks or assertions are needed.
Cause 2 of 6
With that flag on, items[0] is T | undefined because the index may be out of range — which is true. Destructure with a check, or use items.at(0) and handle the undefined case explicitly.
Cause 3 of 6
Both can legitimately miss. Capture the result and check it with a real error: const row = byId.get(id); if (!row) throw new Error('Unknown id ' + id); — the throw both narrows the type for the code below and gives you a debuggable message when it happens.
Cause 4 of 6
value ?? fallback narrows and reads cleanly. Note that || is not the same: it also replaces 0, empty string and false, which is a common source of quiet bugs in numeric code.
Cause 5 of 6
TypeScript discards narrowing for a mutable variable once a callback could have changed it. Copy the checked value into a const first and use that inside the closure.
Cause 6 of 6
value!.id removes the check without proving anything, converting a compile-time error into a runtime TypeError. Use it only for invariants the compiler cannot see, and leave a comment saying why it holds.
Model optionality honestly in your types and narrow once, near the top of a function, so the rest of the body works with values that are known to exist.
user.profile?.email is valid and typed as possibly undefined. It does not make the result defined, so the next thing you do with it may raise the same error one level along.Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.
Open the editor