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

@typesugar/typeclass

v0.1.0

Published

🧊 Scala 3-style typeclass macros with auto-derivation for typesugar

Readme

@typesugar/typeclass

Scala 3-style typeclasses with compile-time resolution.

Overview

@typesugar/typeclass brings Scala's powerful typeclass pattern to TypeScript. Define typeclasses, provide instances, auto-derive implementations, and summon instances at compile time — no runtime dictionary passing overhead.

Installation

npm install @typesugar/typeclass
# or
pnpm add @typesugar/typeclass

Usage

Define a Typeclass

import { typeclass } from "@typesugar/typeclass";

@typeclass
interface Show<A> {
  show(a: A): string;
}

@typeclass
interface Eq<A> {
  equals(a: A, b: A): boolean;
}

@typeclass
interface Ord<A> extends Eq<A> {
  compare(a: A, b: A): -1 | 0 | 1;
}

Provide Instances

import { instance } from "@typesugar/typeclass";

@instance(Show, Number)
const numberShow: Show<number> = {
  show: (n) => n.toString(),
};

@instance(Eq, String)
const stringEq: Eq<string> = {
  equals: (a, b) => a === b,
};

Auto-Derive Instances

import { deriving } from "@typesugar/typeclass";

@deriving(Show, Eq, Ord)
interface User {
  id: number;
  name: string;
}

// Generated instances use JSON-based implementations:
// - userShow: Show<User>
// - userEq: Eq<User>
// - userOrd: Ord<User>

Summon Instances

import { summon } from "@typesugar/typeclass";

// Get a typeclass instance at compile time
const showNumber = summon<Show<number>>();
// Compiles to: numberShow

console.log(showNumber.show(42)); // "42"

Extension Methods with extend()

import { extend } from "@typesugar/typeclass";

// Add typeclass methods to a value
const result = extend(42).show();
// Compiles to: { value: 42, show: () => numberShow.show(42) }

console.log(result.show()); // "42"

Implicit Extension Methods

The transformer can automatically rewrite method calls on types with typeclass instances:

// If User has a Show instance:
user.show();
// Compiles to: userShow.show(user)

API Reference

Attribute Macros

  • @typeclass — Define a typeclass interface
  • @instance(TC, Type) — Provide a typeclass instance
  • @deriving(TC1, TC2, ...) — Auto-derive typeclass instances

Expression Macros

  • summon<TC<T>>() — Get a typeclass instance at compile time
  • extend(value) — Wrap a value with extension methods

Functions

  • getTypeclasses() — Get all registered typeclasses
  • getInstances() — Get all registered instances
  • findExtensionMethod(typeName, methodName) — Find an extension method
  • clearRegistries() — Clear all registries (for testing)
  • register() — Register macros (called automatically on import)

Auto-Derivation

The following typeclasses support auto-derivation with @deriving:

| Typeclass | Generated Implementation | | --------- | ----------------------------- | | Show | JSON.stringify(a) | | Eq | Deep equality via JSON | | Ord | Lexicographic JSON comparison | | Hash | djb2 hash of JSON string |

License

MIT