@alexshelkov/resultdeprecated

Type-safe error handling without exceptions =========================================== Flexible and explicit error handling with a small flavor of functional languages.

Usage no npm install needed!

<script type="module">
  import alexshelkovResult from 'https://cdn.skypack.dev/@alexshelkov/result';
</script>

README

Type-safe error handling without exceptions

Flexible and explicit error handling with a small flavor of functional languages.

TypeScript Test Coverage Status

Result type allows you go from this:

const user = getUser(email);

if (!user) {
    throw new Error("Can't get user") // but what happens, why email is invalid? 
                                      // and who will catch this error?
}

to this:

const userResult = getUser(email);

if (userResult.isErr()) {
  const err = userResult.err();
  
  switch (err.type) {
    case 'InvalidEmail':
      console.log('User email is invalid'); break;
    case 'UserNotExists':
      console.log("Can't find the user"); break;
    case 'UserBannded':
      console.log(`User was bannded: ${err.ban}`); break;
  }
}