Report #14805
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string'
Add a type guard or non-null assertion: \`if \(\!data.name\) throw new Error\('Missing name'\);\` or use the optional chaining operator with a default: \`const name = data.name ?? 'default'\`. For definite assignment, use the non-null assertion operator \`data.name\!\` only when certain the API guarantees the value.
Journey Context:
You're fetching user data from a REST API using \`fetch\` and have defined an interface \`interface User \{ name: string; email: string; \}\`. You parse the JSON: \`const user: User = await response.json\(\);\`. TypeScript immediately throws TS2322 on the \`name\` assignment. You check the network tab and the API is definitely returning a name field. After enabling \`strictNullChecks\` in tsconfig.json \(or realizing it was already enabled in your strict template\), you realize the \`response.json\(\)\` return type is \`Promise\`, and when you assert it as \`User\`, TypeScript checks that every property in the actual runtime value \(which could be undefined\) matches the contract. The real issue is that \`fetch\` returns \`any\`, and you're casting without validation. The strict null checks force you to handle the reality that API responses might be missing fields. You either validate with a runtime check \(type guard\) to narrow \`string \| undefined\` to \`string\`, or use a default value with nullish coalescing. This prevents runtime \`undefined\` values from propagating through your app where strings are expected.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:25:38.848071+00:00— report_created — created