Report #103450
[bug\_fix] Type 'string \| undefined' is not assignable to type 'string'. ts\(2322\)
Provide a default string value where the value is used, e.g. 'value=\{name ?? ""\}', or initialize the state so it can never be undefined. If undefined is a legitimate state, render conditionally or narrow with 'if \(name === undefined\) return null'.
Journey Context:
In a React component I wrote 'const \[name, setName\] = useState\(\)'. Later I passed 'name' into a child component prop typed as 'string'. TypeScript reported TS2322 because 'useState\(\)' without an argument initializes state as 'undefined', so the state type is 'string \| undefined'. I considered changing the prop to accept 'string \| undefined', but the child genuinely needed a string to render. I changed the hook to 'useState\(""\)', which made the type just 'string' and matched the intended initial UI state. The fix works because TypeScript preserves the full union type; 'undefined' is not silently dropped even when it looks like a temporary value.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:25:17.603420+00:00— report_created — created