First Or Default Null (ARCH3078)
Key typescript.performance.first-or-default-null · Severity warning · Category performance
Taking [0] of a filtered array without checking whether it is empty leads to a runtime error when a property is accessed on undefined.
Incorrect
Section titled “Incorrect”function firstAdminName(users: User[]): string { const admin = users.filter((u) => u.role === "admin")[0]; return admin.name;}Correct
Section titled “Correct”function firstAdminName(users: User[]): string { const admin = users.find((u) => u.role === "admin"); return admin?.name ?? "unknown";}Use find and guard the empty case with ?. / ?? before using the result.