Report #13758
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string' under strictNullChecks
Add an explicit null check \(e.g., \`if \(value\)\`\) or use the non-null assertion operator \(\`value\!\`\) if certain of presence, or change the target type to accept undefined. Root cause: With strictNullChecks enabled, TypeScript treats undefined and null as distinct types that must be explicitly handled before assignment to non-nullable types.
Journey Context:
Developer enables \`strict: true\` \(which includes \`strictNullChecks: true\`\) in an existing codebase or uses a library with strict settings. They write \`const name: string = document.getElementById\('name'\)?.value\` or \`const data: string = JSON.parse\(localStorage.getItem\('key'\)\)\`. TypeScript immediately errors with TS2322: "Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'." Developer initially tries casting with \`as string\`, which silences the compiler but risks runtime crashes when the value is actually undefined. They then realize they must guard against the undefined case: they refactor to \`if \(value\) \{ const name: string = value; ... \}\` or use the nullish coalescing operator \`const name = value ?? 'default'\`. For complex object property access, they adopt optional chaining \(\`obj?.prop?.subProp\`\) followed by explicit checks before assignment to strict types.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:43:11.837636+00:00— report_created — created