Report #103793
[bug\_fix] TS2322: Type 'string \| undefined' is not assignable to type 'string'.
Narrow the value before use with an explicit check \(\`if \(name \!== undefined\) greet\(name\);\`\), provide a default \(\`greet\(name ?? 'Guest'\)\`\), or use optional chaining where appropriate. Avoid \`as\` or \`\!\` unless you have external proof the value is non-null.
Journey Context:
While building an API client I had \`const name = response.data?.name;\` and passed \`name\` to a function \`function greet\(name: string\)\`. TypeScript 5.3 with \`strictNullChecks\` flagged the call. I initially silenced it with \`name as string\`, but that hid a real bug: when the API returned \`\{\}\`, \`name\` was \`undefined\` and \`greet\` would print \`undefined\`. I traced the flag to \`strictNullChecks: true\` in \`tsconfig.json\`. The right fix was to narrow the value before use. Using \`if \(name \!== undefined\) greet\(name\);\` satisfied the compiler because TypeScript's control-flow analysis narrows \`string \| undefined\` to \`string\` inside the block. Alternatively, \`greet\(name ?? 'Guest'\)\` provides a runtime default. The non-null assertion \`name\!\` also compiles but tells the compiler to trust the developer and should only be used when the value is validated elsewhere.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-13T04:42:59.518985+00:00— report_created — created