Nostradamus Predicting Maelstroms
    Wondering what’s next for npm?Check out our public roadmap! »

    type-safe-errors
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.3 • Public • Published

    Type-safe errors in TypeScript

    Description

    type-safe-errors is a library that provide type safe errors/exceptions to Typescript.
    [Describe the problem with an example - what is the problem with errors in TS/JS]
    [Describe the proposed solution]

    Table Of Contents

    Installation

    npm i type-safe-errors
    import { Ok } from 'type-safe-errors';
    
    const okValue = Ok.of(5);
    okValue
        .map(val => 2 * val)
        .map((val) => console.log(val));

    Philosphy

    [Describe the project philosphy: minimal API, practical API, async]

    API Documentation

    type-safe-errors expose three objects: Ok, Err and Result.

    Ok

    Ok object represents a valid result of an action.

    Ok.of

    Create new Ok result.

    Signature:

    Ok.of<TValue>(value: TValue): Ok<TValue> {}

    Example:

    import { Ok } from 'type-safe-errors';
    
    const okResult = Ok.of({
      name: "John",
      surname: "Doe"
    });

    Err

    Err object represents an invalid result of an action.

    Err.of

    Create new Err result.

    Signature:

    Err.of<TError>(value: TError): Err<TError> {}

    Example:

    import { Err } from 'type-safe-errors';
    
    class UserNotFound {
      __brand: "UserNotFound"
    }
    
    const errResult = Err.of(new UserNotFound());

    Inspiration

    [Reference Khalil ("https://khalilstemmler.com/"), neverthrow and others]

    Install

    npm i type-safe-errors

    DownloadsWeekly Downloads

    5

    Version

    0.1.3

    License

    ISC

    Unpacked Size

    85.1 kB

    Total Files

    29

    Last publish

    Collaborators

    • avatar