Report #11066
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
With \`strictNullChecks\` enabled, TypeScript enforces that potentially \`undefined\` values cannot be assigned to non-nullable types. The fix is to implement a type guard to narrow the type: check if the value is defined \(e.g., \`if \(val === undefined\) throw ...\` or \`if \(\!x\) return\`\) before assignment. Alternatively, if you are absolutely certain the value is defined at that point \(e.g., after a check elsewhere\), you can use the non-null assertion operator \`x\!\`, or provide a default value using the nullish coalescing operator \`val ?? 'default'\`.
Journey Context:
A developer refactors a legacy JavaScript codebase to TypeScript and turns on \`strict: true\` in \`tsconfig.json\`. Suddenly, hundreds of errors appear. They focus on one: a function \`getUserName\(id: string\): string\` calls another function \`fetchUser\(id\)\` which returns \`Promise\`. They try to return \`user.name\` directly. TS errors: 'Type 'string \| undefined' is not assignable to type 'string'. The developer tries casting with \`as string\`, but the error persists or feels wrong. They try \`// @ts-ignore\`, which works but feels wrong. They realize they need to check if \`user\` exists. They add \`if \(\!user\) throw new Error\('Not found'\)\` and the error vanishes because the type is narrowed. They learn about type narrowing and strict null checks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:21:50.923846+00:00— report_created — created