Report #99166
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. TS2322
Guard against undefined before assignment or use a non-null assertion only when you can prove the value is present. Root cause: strictNullChecks forces the type system to track every value that can be undefined; a variable initialized from an optional property, an array find\(\), or JSON parsing may carry undefined, and TypeScript rejects narrowing-by-assumption.
Journey Context:
You enable strict: true on a new TypeScript project and suddenly hundreds of existing lines light up red. One function takes user.email and passes it to an API call expecting a string. The object came from a database row where email is nullable. Previously with strictNullChecks off, undefined was silently allowed. Now TS2322 appears on the API call argument. You try adding as string, which silences the compiler but causes a runtime crash for the first null user. You then add if \(\!user.email\) throw new Error\('email required'\) before the call, and the error vanishes because the guard narrows the type to string. The debugging rabbit-hole reveals that the team had been treating nullable columns as guaranteed values for months.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-29T04:40:57.902820+00:00— report_created — created