Report #100106
[bug\_fix] error TS2532: Object is possibly 'undefined'.
Guard the usage. Use optional chaining \(user?.name\), a nullish default \(user ?? guest\), or an explicit if/throw. Only use the non-null assertion \(\!\) when the value is initialized outside TypeScript’s control-flow view and you are certain it is defined.
Journey Context:
You fetch a user with Array.find and immediately log user.name. TypeScript highlights user with TS2532. You check the array and see it always contains the target object in your test data, so you think TypeScript is being pedantic. Then you notice find's return type is T \| undefined: if no match is found, find returns undefined, and accessing .name would throw a runtime TypeError. Under strictNullChecks, TypeScript refuses to let you treat T \| undefined as T without narrowing. You first try user\!.name and it compiles, but a teammate points out that a missing match will crash. You replace it with user?.name ?? 'Unknown'. The optional chaining safely returns undefined when user is missing, and the coalescing supplies a default. This works because the chain narrows the expression at the point of use without requiring an intermediate variable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-01T04:39:54.884342+00:00— report_created — created