Report #93566
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. TS2322
Implement a type guard to narrow the type: \`if \(value === undefined\) \{ return; \}\` or \`if \(\!value\) throw new Error\(\)\`. After this check, TypeScript narrows the type to \`string\`. Alternatively, if you are certain the value is defined \(e.g., you just checked it\), use the non-null assertion operator \`value\!\` \(use sparingly\).
Journey Context:
You are fetching data from an API that returns \`User \| null\`. You write \`const user = await fetchUser\(id\);\` and then try to access \`user.name\`, assigning it to a variable typed strictly as \`string\`. TypeScript immediately flags \`user\` with the error: Type 'User \| null' is not assignable to type 'User'. You try to access \`user.name\` anyway and get 'Object is possibly null'. You spend time looking at the API types, confirming it indeed might return null on 404. You consider disabling \`strictNullChecks\` in tsconfig.json to make the error go away, but realize that just hides potential runtime crashes. You then realize you must handle the null case explicitly. You write \`if \(\!user\) \{ throw new Error\('Not found'\); \}\`. Magically, the error below that line disappears because TypeScript's control flow analysis narrowed \`user\` from \`User \| null\` to just \`User\`. The fix works because \`strictNullChecks\` forces explicit handling of null/undefined values, and type guards provide the compiler the proof it needs to narrow the union type.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:38:09.724342+00:00— report_created — created