Dealing with type safety

I am trying to do something like this: if (result.length === 0) { throw new Error(No user with id ${id} found.); } return result[0]; But typescript complains that { .... } | undefined is assignable to { ... }. How could i fix that? Should the if already deal with the case in which result is undefined?
1 Reply
Angelelz
Angelelz15mo ago
You have to assign it to a variable first to make the TS compiler happy
const value = result[0]
if (!value) {
throw new Error(No user with id ${id} found.);
}

return value;
const value = result[0]
if (!value) {
throw new Error(No user with id ${id} found.);
}

return value;
Want results from more Discord servers?
Add your server