Report #10317
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
Narrow the type using a type guard \(e.g., \`if \(x \!== undefined\)\`\), use the non-null assertion operator \`x\!\` only if certain the value is defined, or provide a default value using the nullish coalescing operator \`x ?? 'default'\`.
Journey Context:
A developer enables \`strict: true\` in \`tsconfig.json\` for the first time on a legacy JS codebase. The compiler immediately surfaces hundreds of errors in functions that handle database results or API responses. In one instance, a variable \`displayName\` is typed as \`string \| undefined\` \(because the database column is nullable\), but it is being passed to a function \`formatName\(name: string\)\`. The developer initially considers changing the function parameter to accept \`string \| undefined\`, but this cascades into requiring changes across the entire call stack. After inspecting the data flow, they realize the specific code path guarantees the value is non-null due to a prior check, but TypeScript's flow analysis lost track of it. They apply a type guard \`if \(\!displayName\) return;\` before the call, which narrows the type to \`string\` for the remainder of the block. This fix works because \`strictNullChecks\` treats \`null\` and \`undefined\` as distinct types that must be explicitly handled, and type guards are the idiomatic way to prove to the compiler that a value is defined in a specific scope.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:19:23.696620+00:00— report_created — created