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.1

Published

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

Downloads

458

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 (JSDoc Syntax β€” Preferred)

JSDoc tags work without the preprocessor and provide better tooling support:

/** @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;
}

Operator Mapping with @op

Map typeclass methods to operators using @op JSDoc tags on method signatures:

/** @typeclass */
interface Numeric<A> {
  /** @op + */
  add(a: A, b: A): A;

  /** @op * */
  mul(a: A, b: A): A;

  /** @op - */
  sub(a: A, b: A): A;
}

When an instance exists, operators compile to typeclass method calls:

a + b; // Compiles to: numericInstance.add(a, b)
a * b; // Compiles to: numericInstance.mul(a, b)

Provide Instances

/** @impl Show<number> */
const numberShow: Show<number> = {
  show: (n) => n.toString(),
};

/** @impl Eq<string> */
const stringEq: Eq<string> = {
  equals: (a, b) => a === b,
};

For HKT typeclasses (Functor, Monad), use the same syntax β€” no *F companion needed:

/** @impl Functor<Option> */
const optionFunctor = {
  map: (fa, f) => (fa === null ? null : f(fa)),
};

/** @impl Functor<Either<string>> */  // Partial application: fixes E, varies A
const eitherStringFunctor = { ... };

Auto-Derive Instances

/** @derive Eq, Ord, Show */
interface User {
  id: number;
  name: string;
}

// Generated instances use structural 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)

Alternative Syntax (Decorator)

If you're using the preprocessor, you can also use decorator syntax. This is rewritten to expression macros internally:

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

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

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

API Reference

JSDoc Tags (Preferred β€” No Preprocessor Required)

  • /** @typeclass */ β€” Define a typeclass interface
  • /** @impl TC<Type> */ β€” Provide a typeclass instance
  • /** @derive TC1, TC2, ... */ β€” Auto-derive typeclass instances
  • /** @op <symbol> */ β€” Map a method to an operator (+, -, *, /, ===, etc.)

Attribute Macros (Decorator Syntax)

  • @typeclass β€” Define a typeclass interface (requires preprocessor)
  • @impl(TC, Type) β€” Provide a typeclass instance
  • @derive(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
  • findInstance(typeclassName, forType) β€” Find an instance
  • getTypeclass(name) β€” Get a typeclass definition
  • clearRegistries() β€” Clear all registries (for testing)

Deprecated Syntax

The following patterns still work but are deprecated:

| Deprecated | Preferred | | ------------------------ | -------------------------- | | @instance | @impl | | Op<"+"> return type | @op + JSDoc tag | | instance("TC<T>", obj) | /** @impl TC<T> */ JSDoc | | typeclass("Name") | /** @typeclass */ JSDoc |

Auto-Derivation

The following typeclasses support auto-derivation with @derive:

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

License

MIT