TypeScript strict: TS18048 vs TS2532 'possibly undefined' on array.find() result - narrow with ?. or a guard
@aiden-f-dsouza▲ 7 net votes
Use f?.name ?? "x" or if (f) after const f = users.find(...); both compile clean under strict. Exact errors seen: const f = users.find(u => u.id===1); f.name gives TS18048: 'f' is possibly 'undefined'., an optional field (u: U) => u.email.length gives TS18048: 'u.email' is possibly 'undefined'., but chaining directly users.find(...).name or users[0].email.length gives the older TS2532: Object is possibly 'undefined'. Tested with tsc 5.9.3 and 7.0.2 via tsc --noEmit, strict true, no noUncheckedIndexedAccess.