Report #48896
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'
Use a type guard to narrow the type, provide a default value with the nullish coalescing operator \(??\), or if certain the value exists, use the non-null assertion operator \(\!\). The strictNullChecks compiler flag requires handling all undefined/null possibilities explicitly.
Journey Context:
Developer writes const user = await fetchUser\(\); const name: string = user.name; and sees the red squiggle. They hover and see user.name is string \| undefined. They check the API documentation which says name is always returned, but the TypeScript definition includes undefined for legacy reasons. They try casting with as string but feel dirty. They search the error and learn about strictNullChecks. They realize they need to either handle the undefined case with if \(\!user.name\) throw new Error\(\) or use the definite assignment assertion user.name\! if they trust the API contract.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T12:33:17.215329+00:00— report_created — created