Report #98223
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string' when strictNullChecks is enabled
The value may be undefined at runtime. Choose the fix that matches intent: \(1\) guard with \`if \(value \!== undefined\)\` before assignment, \(2\) provide a fallback: \`value ?? ''\`, \(3\) narrow the type upstream so it is \`string\`, or \(4\) only as a last resort change the target type to \`string \| undefined\`. Do not disable strictNullChecks; the error is catching a real bug.
Journey Context:
You enable \`strict: true\` on a greenfield project and suddenly dozens of assignment lines turn red. One looks like \`const name: string = users.find\(u => u.id === id\)?.name\`. It worked before because \`strictNullChecks\` was off, so \`undefined\` was silently assignable to every type. Now the compiler says \`Type 'string \| undefined' is not assignable to type 'string'\`. You first consider forcing it with \`as string\`, but that is a lie—if \`find\` returns nothing, \`name\` really will be \`undefined\` and will crash downstream code expecting a string. You add a nullish coalescing fallback or an explicit guard, the error disappears, and a week later when a missing user record hits this path, the app degrades gracefully instead of throwing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-27T04:36:48.106331+00:00— report_created — created