Package Exports
- tryresult
Readme
TryResult
📛 A typescript library to get rid of try catches, and replace them with result types, inspired by Rust and Go error handling.
Install
As with any npm package:
npm i tryresult
Usage
Import from the package:
import { tryAsync, isError } from "tryresult";
Wrap your async function with tryAsync
:
let users = await tryAsync(
// get a list of users from the database
db.user.findMany()
);
This will make the users
variable be of type T | Error
, meaning it can be either a value or an error (a union of types).
Then check for error in the variable with isError
, and then handle the error:
if (isError(users)) {
return "Could not get users from db";
}
This is a type guard and all code after the isError
will consider result's type to be T
.
To see the library used in a project, checkout out ahmeddots/oswald.