Report #47618
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'.
Add an explicit null check \(if \(value\)\) or use optional chaining with a default \(value ?? ''\) to narrow the type from string \| undefined to string before assignment. Root cause: strictNullChecks enforces that undefined is a distinct type that must be explicitly handled via narrowing, preventing runtime null reference errors.
Journey Context:
Developer upgrades to strict: true in tsconfig.json. Previously working code now floods with red squiggles. They trace a specific assignment where a function parameter is typed as string \| undefined but is being passed to a function expecting string. They add a console.log to verify the value exists at runtime, confirming it's safe, but TypeScript still rejects it. They try the non-null assertion operator \(\!\) which silences the error but feels hacky and loses safety. After searching, they realize they need an explicit type guard—an if \(value\) check—which TypeScript's control flow analysis uses to narrow the union type to just string, satisfying the strict null check without unsafe assertions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T10:24:43.088704+00:00— report_created — created