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/derive

v0.1.0

Published

🧊 Auto-derive common implementations (Eq, Ord, Clone, etc.) for typesugar

Readme

@typesugar/derive

Syntactic sugar for TypeScript with zero calories.

Overview

Typeclass operations work automatically on any type with derivable structure:

interface User {
  id: number;
  name: string;
  email: string;
}

const alice: User = { id: 1, name: "Alice", email: "[email protected]" };
const bob: User = { id: 2, name: "Bob", email: "[email protected]" };

// Operators just work β€” auto-derived, auto-specialized
alice === bob; // false (compiles to: alice.id === bob.id && ...)
alice < bob; // true  (lexicographic comparison)

// Methods just work too
alice.show(); // "User(id = 1, name = Alice, email = [email protected])"
alice.clone(); // deep copy
alice.toJson(); // JSON serialization

No decorators. No imports. The compiler derives typeclasses from type structure and inlines them to zero-cost code.

Installation

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

Implicit Usage (Default)

Just use operators and methods on your types:

interface Point {
  x: number;
  y: number;
}

const p1: Point = { x: 1, y: 2 };
const p2: Point = { x: 1, y: 2 };

p1 === p2; // true  β€” Eq typeclass
p1 < p2; // false β€” Ord typeclass
p1.show(); // "Point(x = 1, y = 2)" β€” Show typeclass
p1.clone(); // { x: 1, y: 2 } β€” Clone typeclass
p1.hash(); // 12345 β€” Hash typeclass

Everything compiles to direct code:

// p1 === p2 compiles to:
p1.x === p2.x && p1.y === p2.y;

Explicit Derivation (Optional)

Use @derive() to document capabilities in the type definition:

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

@derive(Eq, Ord, Clone, Debug, Hash, Default, Json, Builder)
interface User {
  id: number;
  name: string;
  email?: string;
}

This is purely documentation β€” the same operations work without the decorator.

Available Typeclasses

Eq β€” Equality

interface Point {
  x: number;
  y: number;
}

p1 === p2; // true
p1.eq(p2); // true (method form)

Ord β€” Ordering

interface Version {
  major: number;
  minor: number;
}

v1 < v2; // true (lexicographic by field order)
v1.compare(v2); // -1

Show β€” String Representation

interface User {
  id: number;
  name: string;
}

user.show(); // "User(id = 1, name = Alice)"

Clone β€” Deep Copy

interface Config {
  settings: Map<string, string>;
}

const c2 = c1.clone(); // Deep copy

Hash β€” Hash Code

interface Point {
  x: number;
  y: number;
}

point.hash(); // Consistent number for hash maps

Default β€” Default Value

interface Options {
  enabled: boolean;
  count: number;
}

Options.default(); // { enabled: false, count: 0 }

Json β€” Serialization

interface User {
  id: number;
  name: string;
}

user.toJson(); // '{"id":1,"name":"Alice"}'
User.fromJson(json); // { id: 1, name: "Alice" }

Builder β€” Fluent Builder

interface User {
  id: number;
  name: string;
  email?: string;
}

new UserBuilder().withId(1).withName("Alice").build();

TypeGuard β€” Runtime Type Check

interface User {
  id: number;
  name: string;
}

if (User.isUser(data)) {
  console.log(data.name); // data is typed as User
}

Custom Instances

When you need non-standard behavior:

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

interface User {
  id: number;
  name: string;
  passwordHash: string;  // Should not affect equality
}

@instance
const userEq: Eq<User> = {
  eq: (a, b) => a.id === b.id && a.name === b.name,
};

API Reference

Types

  • DeriveTypeInfo β€” Type information passed to derive macros
  • DeriveFieldInfo β€” Field information within DeriveTypeInfo

Functions

  • createDerivedFunctionName(operation, typeName) β€” Get the conventional function name for a derive operation

Derive Macros

  • EqDerive, OrdDerive, CloneDerive, DebugDerive
  • HashDerive, DefaultDerive, JsonDerive, BuilderDerive
  • TypeGuardDerive

License

MIT