Git Product home page Git Product logo

teav's Introduction

teav

Typescript errors as values

This library takes heavy inspiration from Rust's Either, Option, and Result types.

Installation

npm i teav --save

Documentation

Documentation is available at https://jdeurt.github.io/teav.

Usage

Simple usage

Javascript errors are invisible to Typescript's type system. To solve this, you can use the Result type to clearly indicate when a function may fail.

import { Result } from "teav";

function tryToMultiply(x: number, y: number): Result<number, Error> {
    if (x < 10 && y < 10) {
        return Result.Ok(x * y);
    }

    return Result.Err(new Error("Cannot multiply! Numbers are too big!"));
}

const z = tryToMultiply(10, 11);

if (z.isOk()) {
    console.log("Result: " + z.unwrap());
} else {
    console.log("Uh oh!");
}

Wrapping error-throwing functions

Teav supports automatically converting the result of a function call to a Result.

import { Result } from "teav";
import { myIoOperationSync } from "./my/io/utils";

// `result` will be `Err` if `myIoOperation` throws an error.
const result = Result.unsafeFrom(() => myIoOperationSync());

// `mapOrElse` is just one of the many ways of handling `Result` objects.
const data = result.mapOrElse(
    (error) => {
        // Handle error case.
    },
    (data) => {
        // Handle data.
    }
);

You can also be more explicit about what errors are expected for an additional layer of safety.

import { Result } from "teav";
import { myIoOperationSync, IoError } from "./my/io/utils";

// If `myIoOperationSync` throws an error that is not an instance of IoError,
// that error will be rethrown.
const result = Result.from([IoError], () => myIoOperationSync());

// ...

It also works with async functions.

import { Result } from "teav";
import { myIoOperation } from "./my/io/utils";

const result = await Result.unsafeFromAsync(() => myIoOperation());

const data = result.mapOrElse(
    (error) => {
        // Handle error case.
    },
    (data) => {
        // Handle data.
    }
);

teav's People

Contributors

jdeurt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.