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

@opinionated-ts/error-factory

v1.0.1

Published

Type-safe error factories with automatic, exact type inference for TypeScript.

Readme

@opinionated-ts/error-factory

npm version npm downloads CI License

Create type-safe application errors with exact TypeScript inference.

Define an error once. Reuse it everywhere. Let TypeScript keep the exact shape of every error you create.

❤️ If you find error-factory useful, consider giving the repo a star — it helps support the project.

Why

Application errors should be consistent, reusable, and precisely typed.

@opinionated-ts/error-factory gives you reusable error factories with:

  • Exact type inference — preserve literal values and the resulting error shape without manual type definitions.
  • Reusable factories — define shared error behavior once and create as many typed errors as you need.
  • Controlled defaults — share common messages and context while allowing per-error values.
  • Fixed values — let the factory control values that callers should not change.
  • Structured context — keep public and internal error data organized.
  • Native errors — regular Error instances with cause, instanceof, stack traces, and toJSON().

Example

import { createError } from "@opinionated-ts/error-factory";

// Create an error factory
const ValidationError = createError({
  name: "ValidationError",

  // Default values are used when the caller does not provide a value.
  defaults: {
    message: "Validation failed.",
  },

  // Fixed values are always applied and cannot be overridden by the caller.
  fixed: {
    code: "VALIDATION_ERROR",
  },
});

// Create an error instance, or throw it directly with `throw ValidationError({...})`.
const error = ValidationError({
  message: "The email address is invalid.",
  context: {
    // Public context contains information that can safely be exposed to consumers.
    public: {
      field: "email",
      reason: "invalid_format",
    },

    // Internal context is excluded from `toJSON()`.
    internal: {
      validator: "email-format",
    },
  },
});

// The resulting values and their exact literal types are inferred automatically.
error.name; // "ValidationError"
error.code; // "VALIDATION_ERROR"
error.message; // "The email address is invalid."
error.context.public.field; // "email"
error.context.internal.validator; // "email-format"

error instanceof ValidationError; // true
error instanceof Error; // true

No custom error class. No manual error type. The type is inferred from the factory and the values you provide.

Exact Type Inference

Exact inference does more than keep your errors type-safe. In editors like VS Code, it makes the complete shape of an error immediately accessible while you work.

For example, hovering over error shows its exact inferred type:

const error: ErrorInstance<
  "ValidationError",
  "VALIDATION_ERROR",
  "The email address is invalid.",
  {
    readonly public: {
      readonly field: "email";
      readonly reason: "invalid_format";
    };
    readonly internal: {
      readonly validator: "email-format";
    };
  },
  true
>;

When errors are evaluated later in a switch, if, or similar control flow, Ctrl + Space can surface the exact values and properties available at that point, while type checking keeps the code aligned with the actual error shape.

This makes errors easier to understand, inspect, and handle throughout your codebase without having to navigate back to where they were created.

See Type Inference for more details and examples.

Guides

Related

This project is part of the Opinionated TS ecosystem, and here are some related projects:

  • @opinionated-ts/result — handle failures as typed values so TypeScript can know which errors a piece of code may produce instead of throwing them.

Open Source for the Community

Every project of the Opinionated TS ecosystem is open source and free for anyone to explore, use, or build on.

Contributions are more than welcome — they're the whole point.

  • 💬 Found a bug? Open an issue.
  • ✨ Have an idea? Start a discussion.
  • 🔧 Want to improve something? Send a PR.

Whether it's a typo, a bug fix, a new feature, or an improvement — every contribution helps a lot.

License

MIT