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

@tettekete/du-result

v1.0.0

Published

This class module returns a Result object based on a discriminated union.

Downloads

1

Readme

@tettekete/du-result

Overview

This module provides a way to handle "result" objects (DUResultT) that represent either success or failure.

The prefix "DU" stands for discriminated union.

The type of the .data property in a "result" object (DUResultT) is determined by the boolean value of the .ok (or .ng) property, using a discriminated union.

For a concrete understanding, it's best to look at the example code in the SYNOPSIS section.

Background

My module @tettekete/result was instance-based, which meant that consumer-side code receiving a result object also had to import @tettekete/result and use a type guard function to enable proper type inference for the .data property.

This module reduces that burden by using a pure discriminated union type.

SYNOPSIS

// - - - - - - - - - -
// Provider-side code
import DUResult, { DUResultT } from '@tettekete/du-result';

// Returns a random success or failure object.
function resultMaker(): DUResultT<
	{ name: string; age: number },  // <-- data type for success
	{ message: string }             // <-- data type for failure
> {
	const type = Math.random() < 0.5 ? 'success' : 'failure';
	switch (type) {
		case 'success':
			return DUResult.success({ name: "Bob", age: 123 });

		case 'failure':
			return DUResult.failure({ message: "No one is there." });
	}
}

// - - - - - - - - - -
// Consumer-side code
const r = resultMaker();

if( r.ok )
{
	const who = r.data;
	console.log('OK:', `name: ${who.name} / age: ${who.age}`);
		// You can access the `name` and `age` properties 
		// without any TypeScript warnings.
}
else
{
	const err = r.data;
	console.error('Error:', err.message);
		// You can access the `message` property without
		// any TypeScript warnings.
}

This code is quoted from examples/00.basic.ts.
You can run it with ts-node examples/00.basic.ts to see it in action.

DUResult class

DUResult is the default export, so you can import it under any name. We'll use DUResult here for clarity.

The DUResult class does not generate instances and only provides two static methods.
It essentially acts as a namespace.

Each method returns a DUResultT object instead of a class instance.

success([message [, data]])

Returns an object of type DUResultT<T, never>.

failure([message [, data]])

Returns an object of type DUResultT<never, E>.

DUResultT<T, E> type

A discriminated union type.
Depending on whether the object has ok or ng, the type of the data property will be narrowed to T or E.

Here is the actual type definition:

export type DUResultT<T = unknown, E = unknown> =
	| { readonly ok: true; readonly ng: false; readonly data: T; readonly message: string }
	| { readonly ok: false; readonly ng: true; readonly data: E; readonly message: string };

ok: boolean / readonly

true for success objects. false for failure objects.

ng: boolean / readonly

true for failure objects. false for success objects.

message: string / readonly

An arbitrary string.
If the object is created using DUResult.success() or DUResult.failure(), the default is "success" or "failure" respectively.

data: <T | E> / readonly

Any payload data.
If created via DUResult.success(), it will be of type T; if created via DUResult.failure(), it will be of type E.