TS2532
Object is possibly undefined
Object is possibly 'undefined'.The compiler’s own words. Not translated — this is the string you pasted into a search box.
A property is read directly from an expression whose result may be `undefined`.
Reproduction
function find(): { name: string } | undefined {
return undefined
}
find().nameThe build asserts this emits exactly this code.
Why the compiler says this
The function signature honestly reports that no result is possible. Chaining a property access immediately afterward ignores that branch, and there is no stable variable for control-flow analysis to narrow first.
Fixes
- 01
function find(): { name: string } | undefined { return undefined } const result = find() if (result) { result.name }Store the result, guard it, and use the narrowed variable.
- 02
function find(): { name: string } | undefined { return undefined } const name = find()?.name ?? 'anonymous'Use optional chaining when absence should flow into a fallback value.
Takeaway
Treat `undefined` as a branch to handle, not a warning to silence.
