Report #85706
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. \(TS2322\)
Apply a type guard to narrow the union: 'if \(value \!== undefined\) \{ target = value; \}' or use the non-null assertion operator 'value\!' if certain of initialization \(use sparingly\). Alternatively, enable 'strictNullChecks: false' \(not recommended\) or type the target as 'string \| undefined'.
Journey Context:
With 'strict: true' enabled, you fetch data returning 'User \| undefined'. You destructure 'const \{ name \} = user' or assign 'const userName: string = user.name', but the compiler flags it. You hover and see 'string \| undefined'. You try casting 'as string', but that feels wrong. You add a default 'user.name \|\| 'default'' but still fails because the type remains. You realize you must explicitly check: 'if \(user && user.name\)' before assignment. Once narrowed, TypeScript's control-flow analysis refines the type to just 'string', satisfying the assignment.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:26:26.154375+00:00— report_created — created