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

ts-micro-result

v3.3.0

Published

Lightweight Result type for TypeScript - API contract for BE ↔ FE

Readme

ts-micro-result

Lightweight Result type for TypeScript

API contract for BE ↔ FE. Not a FP monad.

npm version npm downloads license Edge Ready JSON First No Exceptions API Contract

TL;DR

ts-micro-result is a JSON-safe Result type for API boundaries.

  • Designed for BE ↔ FE contracts
  • Works great with Edge / Serverless
  • No classes, no exceptions, no FP chains
  • Errors are data, not thrown
import { ok, err } from 'ts-micro-result'
import type { Result } from 'ts-micro-result'

// Backend returns Result
function getUser(id: string): Result<User> {
  if (!user) return err({ code: 'NOT_FOUND', message: 'User not found' })
  return ok(user)
}

// Frontend receives predictable JSON
const res = await fetch('/api/users/123')
const result = await res.json()  // { ok: true, data: {...} } or { ok: false, errors: [...] }

Should I Use This?

Use ts-micro-result if:

  • You design APIs with explicit error contracts
  • You want predictable BE ↔ FE JSON responses
  • You run on Edge / Serverless (Cloudflare, Vercel, Workers)
  • You need errors as data (loggable, cacheable, replayable)

Do NOT use if:

  • You want map/flatMap/unwrap → use neverthrow
  • You prefer throwing exceptions → use try/catch
  • You need internal-only error handling → use custom patterns

Install

npm install ts-micro-result

Quick Example

import { ok, err } from 'ts-micro-result'
import type { Result } from 'ts-micro-result'

function getUser(id: string): Result<User> {
  const user = db.find(id)
  if (!user) {
    return err({ code: 'NOT_FOUND', message: 'User not found' })
  }
  return ok(user)
}

const result = getUser('123')

if (result.ok) {
  console.log(result.data.name)  // TypeScript knows data is User
} else {
  console.log(result.errors[0].message)
}

Entries

| Entry | Use Case | Size | |-------|----------|------| | ts-micro-result | Full API | ~2.5 kB | | ts-micro-result/lite | Edge Workers, ultra-small bundles | < 1 kB | | ts-micro-result/http | HTTP helpers | ~0.5 kB | | ts-micro-result/types | FE import type only | 0 kB |

API Overview

Factories

ok()                          // Result<undefined> - void operations
ok(data)                      // Result<T>
ok(data, meta)                // Result<T> with meta
okJson(data)                  // Result<T> with strict JsonValue check (optional)
err(error)                    // Err
err([error1, error2])         // Err with multiple errors
okPage(items, pagination)     // Result<T[]> with pagination

Note: ok() accepts any type - JSON-safety is by convention. Use okJson() if you want compile-time enforcement that data extends JsonValue.

ok() vs ok(null):

  • ok() → void operations (DELETE, UPDATE) → Result<void>
  • ok(null) → null is valid data → Result<T | null>

Utilities

match(result, { ok, err })    // Pattern matching
isResult(value)               // Runtime type guard
isOk(result) / isErr(result)  // Type guards
combine([r1, r2, r3])         // Combine Results into array
combineObject({ a: r1 })      // Combine Results into object
serialize(result)             // Deep JSON-safety

HTTP (ts-micro-result/http)

toHttpResponse(result)        // ok(data) → 200, ok() → 204, err() → 400
sendResponse(res, response)   // Express/Fastify helper
created(result)               // 201
accepted(result)              // 202

// Adapter for automatic error-to-status mapping
const toHttp = withFallbackStatus(createHttpResultAdapter(errorMap), 400)

| Result | Status | Body | |--------|--------|------| | ok(data) | 200 | JSON | | ok() | 204 | null | | err() | 400 | JSON |

Types

type Result<T = undefined> = Ok<T> | Err

interface Ok<T> {
  readonly ok: true
  readonly data: T
  readonly meta?: ResultMeta
}

interface Err {
  readonly ok: false
  readonly errors: readonly ErrorDetail[]
  readonly meta?: ResultMeta
}

interface ErrorDetail {
  readonly code: string      // Machine-readable
  readonly message: string   // Human-readable
  readonly field?: string    // For validation
}

interface ResultMeta {
  readonly pagination?: Pagination
  readonly traceId?: string
  readonly params?: Record<string, unknown>  // Opaque metadata passthrough
}

About params

params is opaque metadata passed through the Result. The library never interprets or mutates it.

Use it for domain-specific data that doesn't fit standard fields:

// Rate limiting
return err(
  { code: 'RATE_LIMIT_EXCEEDED', message: 'Too many requests' },
  {
    traceId,
    params: {
      retryAfter: 60,
      timestamp: Date.now(),
      limit: 'user_login',
    },
  }
)

// Cache hints
return ok(user, {
  params: { cacheKey: `user:${user.id}`, ttl: 3600 },
})

✔ Typed
✔ Keeps message clean
✔ Doesn't leak HTTP concerns
✔ Doesn't break Result shape

Documentation

Comparison

| Library | Purpose | |---------|---------| | ts-micro-result | API boundaries, Edge/Serverless, JSON contracts | | neverthrow | Internal logic, FP patterns, map/flatMap | | fp-ts | Pure FP, effect systems | | Zod / Valibot | Input validation | | RFC 7807 | HTTP-level error format (can use inside err()) |

License

MIT