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 🙏

© 2025 – Pkg Stats / Ryan Hefner

neverthrow-safe-ret

v1.0.1

Published

Adds safeRet() functionality to Ok & Err

Readme

neverthrow-safe-ret

** Type-safe error handling with Go-style tuple returns for neverthrow **

This library extends neverthrow to provide a Go-inspired error-handling pattern while maintaining strict type safety. If you’ve ever wanted to destructure a Result into a [error, value] tuple without losing type information, this is for you.


Features

  • Go-style tuple returns: Use [err, value] destructuring with type inference.
  • Zero dependencies: Works alongside neverthrow, no bloat.
  • Monkey-patch integration: Add .safeRet() directly to Result instances.
  • Type enforcement: Forces error checks at compile time via undefined narrowing.
  • Seamless interop: Works with existing neverthrow code.

Installation

npm install neverthrow-safe-ret  

Why?

The original neverthrow issue #614 highlights the friction of chaining .andThen() for sequential operations. This library solves it by:

  1. Letting you exit early on errors without callback hell.
  2. Inferring types after checks (e.g., value becomes non-undefined post-error guard).

Usage

Basic Example

import { ok, err } from 'neverthrow';  
import 'neverthrow-safe-ret';  

const result = ok(42).safeRet();  
const [error, value] = result;  

if (error) {  
  // Handle error (type: Error | undefined)  
} else {  
  console.log(value); // Type: number  
}  

Real-World Chain

import { Result, ok, err } from 'neverthrow';  
import 'neverthrow-safe-ret';  

declare function getUser(id: string): Result<User, Error>;  
declare function validate(user: User): Result<boolean, ValidationError>;  

function processUser(id: string): Result<boolean, Error | ValidationError> {  
  const [fetchErr, user] = getUser(id).safeRet();  
  if (fetchErr) return err(fetchErr);  

  const [validationErr, isValid] = validate(user).safeRet();  
  if (validationErr) return err(validationErr);  

  return ok(isValid);  
}  

How It Works

The .safeRet() method returns a tuple where:

  • Error position: E (undefined if Ok).
  • Value position: T (undefined if Err).

Type narrowing example:

const [e, s] = ok("string").safeRet();
// e: undefined, s: string
const [e2, s2] = err("error").safeRet();
// e2: "error", s2: undefined

function a(x: Result<number, string>): Result<number, string> {
  const [e3, s3] = x.safeRet();
  // e3: string | undefined, s3: number | undefined
  // this forces you to check for e3
  if (typeof e3 !== "undefined") {
    // here we are sure that e3 is string and s3 is undefined
    return err(e3);
  }
  // s3 is now number
  return ok(s3);
}

Comparison

| Approach | Code Style | Type Safety | Error Checks Enforced | | :-- | :-- | :-- | :-- | | Vanilla neverthrow | Method chaining | ✅ | ❌ (via match) | | neverthrow-safe-ret | Imperative checks | ✅ | ✅ (via undefined) |


Caveats

  • Monkey-patching: Modifies Result prototypes. Avoid if your team dislikes patching.

Contributing

PRs welcome!


Credit: Inspired by neverthrow#614. License: MIT