Tech
Functional Error Handling in TypeScript: Beyond try-catch
Every TypeScript developer has written something like this:
function getUser ( id : string ): User {
const user = db . findUser ( id );
if ( ! user ) throw new Error ( " User not found " );
return user ;
}
The function signature says it returns a User . But that's a lie. It might throw, and nothing in the type system warns you about it. The caller has to remember to wrap it in try-catch , hope they handle the right err...
Read the full discussion on Dev.to
This article was aggregated from Dev.to. Click to join the conversation.
View on Dev.to