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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@firesquid/result

v1.0.0

Published

A simple result type that allows for error handling patterns similar to Go

Readme

Result

Result is a simple (40 lines) typescript library for writing a result type inspired by Go's multi-return pattern of the value and an error. Built to be typesafe and expects you to use typescript (really, there's no point in this unless you do). I find myself repeating a version of these types in lots of projects, so I put them in one repo.

Install

Just copy and paste the code in index.ts into your project. Alternatively, if you love adding flaky dependencies to your app:

npm install @firesquid/result

Example

import type { Result } from ".";
import { some, none, panic } from "."

function myFailableFunction(): Result<number> {
  // foo is just some imaginary function that returns a number
  const myValue = foo();
  
  if (myValue < 0) {
    // something has gone wrong!
    //
    // we can give none an Error or a string. If given a string, it will
    // automatically turn it into an Error
    return none("myValue was less than 0.");
  }

  return some(myValue); 
}


function main() {
  // the const here is important
  const [val, err] = myFailableFunction();
  
  // if we type:
  // val;
  //
  // The editor recognizes its type as number | null and will not let you
  // use it


  if (err !== "OK") {
    // This error is irrecoverable and represents a completely screwed state
    // We can use panic to signify this
    //
    // If the error is recoverable, you should return here instead
    panic(err);
  }

  // the type system recognizes that val is a number and cannot be null at 
  // this point. We can do whatever we want since we're on the happy
  // path
  let anotherValue = val + 1;

  console.log(anotherValue);
}

More

The example above demonstrates use of some, none, Result, and panic. This library also includes:

  • AsyncResult - a handy alias for Promise<Result> because I think the latter is ugly and less readable. Some may disagree and can throw it away
  • VoidResult - just "OK" | Error. Useful for functions that are void but could also fail with an error message. Just like how Result has the methods some and none for constructing it, VoidResult has success and failure for its respective possibilities.

Source Code

For copying and pasting convenience, here's all 40 lines that make up the source:

export type Result<T> = None | Some<T>;
export type AsyncResult<T> = Promise<Result<T>>;

export type None = [null, Error];
export type Some<T> = [T, "OK"];

export type VoidResult = "OK" | Error;
export type AsyncVoidResult = Promise<VoidResult>;

export function some<T>(data: T): Some<T> {
  return [data, "OK"];
}

export function none(error: string | Error): None {
  if (typeof error === "string") {
    error = new Error(error);
  }

  return [null, error];
}

export function panic(error: Error): never {
  console.error("Panicked:", error);
  const e = new Error();
  console.error("Callstack:", e.stack);
  process.exit(1);
}


export function success(): VoidResult {
  return "OK";
}

export function failure(err: string | Error): VoidResult {
  if (typeof err === "string") {
    err = new Error(err);
  }

  return err;
}