Report #87264
[bug\_fix] TS2532: Object is possibly 'undefined'. OR Type 'string \| undefined' is not assignable to type 'string'
Enable strict null checks only after adding explicit null checks, type guards \(e.g., \`if \(x \!= null\)\`\), or optional chaining \(\`x?.prop\`\) to safely handle potentially undefined values. Use non-null assertion \`x\!\` only as a last resort with justification.
Journey Context:
Developer decides to improve code quality by enabling \`strict: true\` or specifically \`strictNullChecks: true\` in \`tsconfig.json\`. Immediately upon running \`tsc\`, they are greeted with hundreds of errors in previously 'working' code. For example, \`const config = loadConfig\(\); console.log\(config.apiKey\);\` now errors with "Object is possibly 'undefined'" because \`loadConfig\(\)\` returns \`Config \| undefined\`. The developer initially tries to silence the error by using the non-null assertion \`config\!.apiKey\`, but linting rules or code review reject this as unsafe. They then explore type guards: \`if \(\!config\) throw new Error\("Config missing"\);\` which narrows the type to \`Config\` within the block. They also discover optional chaining: \`config?.apiKey ?? 'default'\`, which safely returns undefined if config is null/undefined. The root cause realization is that without \`strictNullChecks\`, TypeScript effectively ignored \`null\` and \`undefined\` in type assignments, allowing potential runtime crashes to pass type checking. The fix is not to disable the flag, but to explicitly handle the null cases that were always potentially there.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T05:03:48.491700+00:00— report_created — created