Report #76050
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string' \(strictNullChecks\)
Add an explicit type guard \(\`if \(typeof val === 'string'\)\`\), use the non-null assertion operator \(\`val\!\`\), or provide a default using nullish coalescing \(\`val ?? 'default'\`\).
Journey Context:
Developer fetches data from an API typed as \`User \{ name: string \| undefined \}\`. They destructure \`const \{ name \} = user\` and immediately assign it to \`const displayName: string = name\`. TypeScript throws TS2322. The developer is confused because they "know" the API always returns a name. They try \`if \(name\)\` but the error persists in the block \(perhaps due to variable scoping or reassignment\). They resort to \`name as string\` which silences the error but feels unsafe. They eventually realize \`strictNullChecks\` requires the compiler to prove the value is non-null, necessitating explicit narrowing or the non-null assertion operator when they have out-of-band knowledge.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T10:14:44.129246+00:00— report_created — created