Report #52599
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string' \(TS2322 under strictNullChecks\)
Implement a type guard to narrow the type before assignment. Use an 'if' check \('if \(x\) \{ y = x; \}'\), optional chaining \('x?.toString\(\)'\), the nullish coalescing operator \('x ?? defaultValue'\), or a non-null assertion \('x\!'\) if you have external knowledge that the value is never undefined in this context.
Journey Context:
Developer enables 'strict': true in tsconfig to improve code quality. Immediately, assignments like 'const name: string = user.profile.name;' fail compilation. The error indicates that 'user.profile.name' is typed as 'string \| undefined' \(perhaps from a Prisma schema or GraphQL type\). Developer first tries type casting '\(user.profile.name as string\)' which silences the error but feels unsafe. They read the TypeScript documentation on narrowing and realize they must explicitly handle the undefined case. They refactor to 'if \(user.profile.name\) \{ fullName = user.profile.name; \}' which narrows the type to 'string' within the block. Alternatively, they use 'const name = user.profile.name ?? 'Anonymous';' which provides a default and satisfies the compiler. This fix works because strictNullChecks forces the developer to model the possibility of missing data explicitly, preventing runtime 'undefined' errors.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:47:04.662299+00:00— report_created — created