Report #13610
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. TS2322
Enable strictNullChecks \(or strict\) in tsconfig.json. Then, add a runtime check \(if \(x === undefined\) throw ...\) to narrow the type, use the non-null assertion operator \(x\!\) only if certain it is defined, or change the target type to allow undefined \(string \| undefined\).
Journey Context:
Your team decides to enable strict: true in tsconfig.json to catch more bugs. Immediately, hundreds of errors appear. One persistent error is in a data parsing function where you fetch a user: const name: string = user.name;. TypeScript complains that user.name might be undefined. You look at the API types and realize name?: string. You think about just slapping as string on it, but that defeats the purpose. You consider user.name\!, but worry about runtime crashes. You realize the proper fix is to check if user.name exists first: if \(\!user.name\) throw new Error\('Name required'\); After this check, TypeScript narrows the type to string and the assignment works. You apply this pattern across the codebase, fixing potential null reference exceptions before they hit production.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:14:38.459192+00:00— report_created — created