Skip to main content

TypeScript · 6 common causes

TS2532: Object is possibly 'undefined'

Updated

Under strictNullChecks the value's type includes undefined, and you used it without ruling that out.

The exact message

src/report.ts:11:3 - error TS2532: Object is possibly 'undefined'.

11   user.profile.email;
     ~~~~~~~~~~~~

Found 1 error in src/report.ts:11

Paths, 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'.

How to fix it

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.

Cause 2 of 6

An index access under noUncheckedIndexedAccess.

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

A Map.get() or Array.find() result.

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

You want a default rather than a branch.

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

Narrowing was lost across a closure or an await.

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

You are reaching for the non-null assertion.

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.

Stopping it coming back

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.

TS2532: Object is possibly 'undefined' — common questions

What is the difference between TS2532 and TS18048?
They describe the same situation at different granularity — TS18048 names the specific value, as in "'user.profile' is possibly 'undefined'", while TS2532 reports the object generally. Newer compilers prefer the more specific message. The fix is identical.
Is optional chaining enough to satisfy the compiler?
Yes for reading — 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.
Should I turn on noUncheckedIndexedAccess?
It is stricter than most codebases expect and adds noise around array access, but it is correct: an index really can be out of range. Turn it on for new code and for anything handling untrusted input.

Test the fix before you paste it back

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