Report #21569
[bug\_fix] TS2345: Argument of type 'string \| undefined' is not assignable to parameter of type 'string'.
Add a type guard check \(e.g., \`if \(value\) \{ process\(value\); \}\`\) or use the non-null assertion operator \(\`value\!\`\) if certain the value is defined, because \`strictNullChecks\` enforces that undefined/null are distinct types that must be explicitly narrowed before assignment to non-nullable types.
Journey Context:
You have a function \`processData\(data: string\)\` and you retrieve a value from an API response or localStorage: \`const item = localStorage.getItem\('key'\);\` which returns \`string \| null\`. When you call \`processData\(item\)\`, TypeScript throws TS2345. You check the value at runtime with console.log and see it exists, so you're confused why TypeScript complains. You try casting with \`as string\`, which works but feels unsafe. You consider disabling \`strictNullChecks\` in tsconfig.json, but that removes null-safety across the entire codebase. Realizing that \`strictNullChecks\` treats null and undefined as actual types that must be handled, you understand that TypeScript is forcing you to prove that the value is not null before passing it. You implement a type guard: \`if \(item\) \{ processData\(item\); \}\` which narrows the type from \`string \| null\` to \`string\` within that block. Alternatively, when you have domain knowledge that the value MUST exist \(e.g., initialized earlier\), you use the non-null assertion operator \`item\!\` to tell the compiler to trust you, acknowledging the risk of runtime errors if you're wrong.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T14:36:51.803982+00:00— report_created — created