Report #38222
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. ts\(2322\)
Implement a type guard to narrow the type before assignment. Use an \`if \(value \!== undefined\)\` check, the nullish coalescing operator \`value ?? defaultValue\`, or optional chaining \`value?.prop\`. Alternatively, change the target variable's type to accept \`undefined\` if the logic permits.
Journey Context:
You enabled \`"strict": true\` in tsconfig.json to harden your codebase against null pointer exceptions. Immediately, TypeScript flags a line: \`const displayName: string = user.profile.name;\` with TS2322. You check the \`User\` interface and see \`name?: string\`, making the type \`string \| undefined\`. Previously with \`strictNullChecks\` off, TypeScript silently allowed this. You consider using a non-null assertion \`user.profile.name\!\` to silence the error, but realize that just reintroduces the runtime crash risk you wanted to avoid. You instead refactor to \`const displayName = user.profile.name ?? 'Anonymous';\`. TypeScript now sees the resulting type as \`string\` because the nullish coalescing handles the undefined case explicitly. The error disappears and the code is now crash-proof.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:38:04.031989+00:00— report_created — created