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?
Was this page helpful?