Report #99641
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. ts\(2322\)
Narrow the value before assignment using a type guard \(\`if \(value\)\`\), optional chaining \(\`obj?.prop ?? ''\`\), or an explicit fallback. Avoid the non-null assertion \(\`\!\`\) unless you have just proven the value is non-null at runtime.
Journey Context:
You fetch a user profile from a JSON API and type it as \`\{ name: string; bio?: string \}\`. In the component you write \`const displayName: string = user.bio\` and TS reports 2322. You first think the type is wrong and add \`bio: string\` to the interface, but the backend actually omits the field sometimes and now runtime crashes appear. You then try \`as string\`, which silences the compiler but causes \`undefined\` to render as the literal string "undefined". After checking the network tab you realize the field is genuinely optional. You refactor to \`const displayName = user.bio ?? 'No bio provided'\`, which satisfies both the compiler and runtime. The root cause is that \`strictNullChecks\` forces the type system to track \`undefined\` separately from the base type; assignment to a non-optional type requires proof the value exists.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:48:52.527976+00:00— report_created — created