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

ts-err-ok

v1.0.0

Published

Typesafe result and error handling for typescript

Downloads

2

Readme

ts-err-ok

Javascript exception handling leaves much to be asked. That is why this package provides a method for typesafe error handling. When combined with typescript type coercion you are left with a powerful primitive to signal the results of operations.

Install

$ npm install ts-err-ok

Usage

Result declares your possible return values. In this case number if ok, else Error.

function divide(x: number, y: number): Result<number, Error>;

The compiler forces you to check whether the operation was successful or not.

const result = divide(10, 5);
if (result.ok()) {
  // Compiler coerces "result" into Ok<number>.
  console.log("Result" + result.val);
} else {
  // Compiler coerces "result" into Err<Error>.
  console.log("Error" + result.error);
}
// This results in a type error because the result has to explicitly checked before either "val" or "error" can be accessed.
console.log("Result: ", result.val);

Build result objects by using the helper functions ok() and err().

function divide(x: number, y: number): Result<number, Error> {
  if (y === 0) {
    // Use err() to construct an error result.
    return err(new Error("Division by zero"));
  } else {
    // Use ok() to oconstruct an ok result.
    return ok(x / y);
  }
}

Examples

By using discriminated unions as your error types you can get very fine grained error handling. An example that parses songs out of json strings

interface Song {
  author: string;
  title: string;
}

interface MissingField {
  type: "MissingField";
  message: string;
}

interface JsonSyntaxError {
  type: "JsonSyntaxError";
  syntaxError: SyntaxError;
}

type SongParseError = MissingField | JsonSyntaxError;

export function parseSong(str: string): Result<Song, SongParseError> {
  try {
    const obj = JSON.parse(str);
    if (obj.author === undefined) {
      return err({
        type: "MissingField",
        message: "Author was undefined",
      });
    }
    if (obj.title === undefined) {
      return err({
        type: "MissingField",
        message: "Title was undefined",
      });
    }
    return ok(obj);
  } catch (error) {
    if (error instanceof SyntaxError) {
      return err({
        type: "JsonSyntaxError",
        syntaxError: error,
      });
    } else {
      // Rethrow unexpected errors.
      throw error;
    }
  }
}

const res = parseSong("{[]");
if (res.ok()) {
  // "val" has type "Song".
  console.log("Song: ", res.val);
} else {
  switch (res.error.type) {
    case "MissingField":
      // "error" has type "MissingField".
      console.log("Missing field: ", res.error.message);
      break;
    case "JsonSyntaxError":
      // "error" has type "SyntaxError".
      console.log("Syntax error: ", res.error.syntaxError);
      break;
  }
}

Maintainers

License

Other

Projects that use ts-err-ok