Report #94405
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
Add a runtime guard to narrow the type: \`if \(\!value\) throw new Error\('Value required'\);\` or use the non-null assertion operator \`value\!\` if certain it is defined. Alternatively, change the target type to \`string \| undefined\`. Root cause: \`strictNullChecks\` \(enabled via \`strict: true\`\) distinguishes between nullable and non-nullable types, preventing assignment of potentially undefined values to variables expecting a concrete type.
Journey Context:
Developer enables \`strict: true\` in an existing codebase to improve type safety. Immediately, dozens of TS2322 errors appear. One occurs at \`const name: string = user.profile.name;\`, where \`user.profile\` might be undefined. The developer initially tries to cast: \`as string\`, but the error persists or feels wrong. They search the error message and find references to \`strictNullChecks\`. They realize that TypeScript is now modeling the possibility of null/undefined values that were previously silently ignored. They refactor to add a guard: \`if \(\!user.profile\) throw new Error\('Profile missing'\); const name = user.profile.name;\`. The error vanishes, and the developer appreciates that a potential runtime null pointer exception was caught at compile time.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:02:40.007132+00:00— report_created — created