npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@colinlogue/ts-result

v0.1.2

Published

Type-safe error handling in TypeScript

Downloads

1

Readme

TypeScript Result type

Defines a Result type for type-safe error handling, similar to the same type in Elm or Rust.

API

The following functions are exported from the module:

/**
 * Create a Result from the given value.
 * @param val The value to hold in the Result.
 * @returns A new Result object.
 */
function ok<T>(val: T): Result<T,any>;

/**
 * Create a Result from the given error.
 * @param error The error to hold in the Result.
 * @returns A new Result object.
 */
function err<E>(error: E): Result<any,E>;

/**
 * Flatten a Result that contains another Result.
 * @param res A Result that may hold another Result.
 * @returns A new Result object.
 */
function join<T,E>(res: Result<Result<T,E>,E>): Result<T,E>;

/**
 * Transform a function that may throw into a function that returns a Result
 * without throwing.
 * @param f A function that may throw.
 * @param liftError A function that provides an error value if `f` throws.
 * @returns A function that takes the same input as `f` but returns a Result.
 */
function lift<T,T2,E>(f: (val: T) => T2, liftError: (e: unknown) => E): (val: T) => Result<T2,E>;

Note that the only way to create a value of type Result is with these functions, as the underlying classes are not exposed.

Values of type Result have the following interface:

/**
 * True when the Result holds a value, false when it holds an error.
 */
ok: boolean;

/**
 * Only present on the Ok variant, when the `ok` property is `true`.
 */
value: T;

/**
 * Only present on the Err variant, when the `ok` property is `false`.
 */
error: E;

/**
 * Transform the value using a given function, if the Result holds a value.
 * @param f The function to apply to the value.
 * @returns A new Result object.
 */
map<T2>(f: (val: T) => T2): Result<T2,E>;

/**
 * Transform the error using a given function, if the Result holds an error.
 * @param f The function to apply to the error.
 * @returns A new Result object.
 */
mapError<E2>(f: (error: E) => E2): Result<T,E2>;

/**
 * Transform the Result using one of the given functions depending on whether
 * the Result holds a value or an error.
 * @param f1 The function to apply if the Result holds a value.
 * @param f2 The function to apply if the Result holds an error.
 * @returns A new Result object.
 */
mapBoth<T2,E2>(f1: (val: T) => T2, f2: (error: E) => E2): Result<T2,E2>;

/**
 * Chain together multiple functions that return a Result.
 * @param f1 The function to apply if the Result holds a value.
 * @param f2 An optional function to apply if the Result holds an error.
 * @returns A new Result object.
 */
then<T2>(f1: (val: T) => Result<T2,E>, f2?: (error: E) => Result<T2,E>): Result<T2,E>;

/**
 * Recover from a potential error. Allows partial recovery by mapping error
 * values to either an Ok value or an Err that may optionally hold a different
 * error type.
 * @param f The function to apply if the Result holds an error.
 * @returns A new Result object.
 */
recover<E2>(f: (error: E) => Result<T,E2>): Result<T,E2>;

/**
 * Attempts to transform the value using the given function, if the Result
 * holds a value. If an error is thrown, the provided handler catches it and
 * transforms into the appropriate error type.
 * @param f A function that may throw.
 * @param handleError A function to handle the error if `f` throws.
 * @returns A new Result object.
 */
try<T2>(f: (val: T) => T2, handleError: (e: unknown) => E): Result<T2,E>;

/**
 * Get the value from the Result if it holds one, or a default value it not.
 * @param val The default value to use if the Result holds an error.
 */
withDefault(val: T): T;

/**
 * Get the value from the Result if it holds one, or transform the error into
 * a value if not.
 * @param f The function to apply if the Result holds an error.
 */
catch(f: (error: E) => T): T;

/**
 * Transform the Result into a Promise that either resolves to the value if it
 * holds one, or rejects with the error as the reason otherwise.
 */
asPromise(): Promise<T>;