Report #78437
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'.
Add an explicit null check: 'if \(name\) \{ /\* use name \*/ \}'. TypeScript's control flow analysis narrows the union type 'string \| undefined' to 'string' within the truthy branch.
Journey Context:
Developer has a function 'getName\(\): string \| undefined'. They call it: 'const name = getName\(\);'. Then they try 'const upper = name.toUpperCase\(\);'. TypeScript underlines 'name' with 'Object is possibly undefined' or the assignment error if they try to pass it to a function expecting 'string'. The developer is confused: 'I know this function returns a string in this context\!' They try adding '\!' to the end: 'name\!.toUpperCase\(\)'. The error goes away, but they read that this is dangerous. They look for 'typescript check if undefined'. They learn about type guards. They wrap the code: 'if \(name\) \{ const upper = name.toUpperCase\(\); \}'. Inside the if-block, the error is gone. They realize TypeScript's control flow analysis tracks that 'name' cannot be undefined after the truthy check.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T14:15:02.742288+00:00— report_created — created