Report #3615
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
Enable strictNullChecks \(or strict mode\) in tsconfig.json and refactor the code to handle the undefined case explicitly using a type guard \(if \(x \!== undefined\)\), optional chaining, or non-null assertion operator \(\!\) when absolutely certain. The root cause is that with strictNullChecks enabled, TypeScript treats undefined and null as distinct types that must be explicitly narrowed before assignment to non-nullable types.
Journey Context:
You just enabled \`strict: true\` in your tsconfig.json to improve type safety. Immediately, hundreds of errors appear. One particularly stubborn one occurs in a data parsing function: \`const userName: string = response.data.name;\`. The error says \`Type 'string \| undefined' is not assignable to type 'string'\`. You check the API response type and see \`name?: string\`. You try adding \`if \(response.data.name\)\` above the assignment, but the error persists because TypeScript doesn't recognize that the check guarantees the value for the subsequent line \(perhaps due to a closure or because you're destructuring\). You try casting with \`as string\`, which silences it but feels wrong. You search Stack Overflow and find that \`strictNullChecks\` requires explicit narrowing. You refactor to: \`const userName = response.data.name; if \(userName === undefined\) throw new Error\('No name'\);\` and then use \`userName\` \(now typed as \`string\`\). Alternatively, you use the non-null assertion \`response.data.name\!\` when you're certain the API contract guarantees presence despite the optional type, but you document why. The error disappears because TypeScript now sees a guaranteed string.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T17:46:00.097627+00:00— report_created — created