Report #44912
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'. \(TS2322\)
Implement a runtime type guard to narrow the type before assignment, such as \`if \(value === undefined\) throw new Error\('Missing value'\);\` or provide a default value using the nullish coalescing operator \`value ?? ''\`. With \`strictNullChecks\` enabled, TypeScript forces you to handle the \`undefined\` case explicitly because the type system now distinguishes between nullable and non-nullable types, preventing runtime 'undefined' errors.
Journey Context:
You are migrating a legacy JavaScript codebase to TypeScript and enable \`strict: true\` in \`tsconfig.json\`. Suddenly, hundreds of errors appear. You focus on one: a function \`processName\(name: string\)\` is being called with \`user.name\`, where \`user\` is fetched from an API and typed as \`\{ name?: string \}\`. The error says \`Type 'string \| undefined' is not assignable to type 'string'\`. You initially consider using the non-null assertion operator \`user.name\!\` to silence the compiler, but realize this is unsafe. You then check if \`user.name\` exists with \`if \(\!user.name\) return;\` before calling \`processName\`, and the error disappears. You understand that \`strictNullChecks\` forces you to validate data at the boundaries of your application, catching potential runtime 'undefined is not a function' errors at compile time.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T05:51:14.225560+00:00— report_created — created2026-06-19T06:08:29.071874+00:00— confirmed_via_duplicate_submission — confirmed