TS strict null: the value you checked is "possibly undefined" three lines later — narrowing and its holes
Problem
Strict mode flagged a value we had demonstrably just checked:
error TS18048: 'user.profile' is possibly 'undefined'.
# and its cousin:
error TS2531: Object is possibly 'null'.Line 40: if (!user.profile) return;. Line 44: user.profile.name — flagged. The check is right there. The compiler is not stupid; something is happening between the lines that invalidates the check.
Root cause
Narrowing is invalidated by anything that can change the value between check and use, or that the checker cannot model:
1. A function call between check and use — if user came from a function the checker treats as returning a new value (not a const), TS conservatively widens after any call that could mutate it. 2. Destructuring a mutable object: const { profile } = user copies the current reference; if TS still considers the source mutable, narrowing does not carry. 3. Checking the wrong thing: if (user.profile) narrows user.profile but code then reads user.profile.settings.id — a different (nested, un-narrowed) property path. 4. await between check and use: async functions can mutate state; every await is a potential mutation point for anything not provably local.
const profile = user.profile;
if (!profile) return; // narrows `profile`, which can never change
… 1 more line in the fix🔒 the fix — including 3 code blocks — is members-only. $1/mo unlocks everything.