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

@raubjo/compose

v0.1.1

Published

Trait-style multiple inheritance for TypeScript classes. Merges instance properties and prototype methods from several classes into one, and throws on name collisions instead of silently overwriting them.

Readme

@raubjo/compose

Trait-style multiple inheritance for TypeScript classes. Merges instance properties and prototype methods from several classes into one, and throws on name collisions instead of silently overwriting them.

bun install

Usage

import { Compose } from "@raubjo/compose"

class HasAttributes {
	attrs: Record<string, unknown> = {}
}

class HasCasts {
	cast(value: number) {
		return value * 2
	}
}

class Model extends Compose.with(HasAttributes, HasCasts) {}

const model = new Model()
model.attrs.name = "hi"
model.cast(3) // 6

Compose.with(...traits) also accepts an array, Compose.with([HasAttributes, HasCasts]), for cases where the trait list is built dynamically.

Trait classes are used as plain property/method bags:

  • Instance fields (assigned in the trait's constructor, e.g. attrs = {}) are copied onto each new instance.
  • Prototype methods and accessors (get/set) are copied onto the composed class's prototype, so they're shared across instances like normal methods.
  • Trait constructors run (so instance fields get initialized), but any arguments passed to new Model(...) are only forwarded to Compose's own constructor, not to the traits.

Dependencies

A trait can declare other traits it needs via a static requires list. The caller only has to ask for the capability it wants; requires is expanded automatically:

class HasAttributes {
	attributes: Record<string, unknown> = {}
}

class HasCasts {
	static requires = [HasAttributes] as const

	cast(value: number) {
		return value * 2
	}
}

class Model extends Compose.with(HasCasts) {}

const model = new Model()

model.attributes.name = "Example" // typechecks, even though HasAttributes
model.cast(3) // 6                // was never passed to Compose.with()
  • requires is resolved recursively — a required trait's own requires are pulled in too.
  • A shared dependency is only composed once, however many traits require it, and deduplication is by constructor identity (not class name).
  • Dependencies are always composed before the trait that requires them. Unrelated traits otherwise keep the order you passed to Compose.with().
  • A dependency cycle (A requires B requires A) throws instead of recursing forever: Composition dependency cycle: A -> B -> A.
  • requires must be declared explicitly. Extending a trait with extends is plain JS inheritance and is never treated as a composition dependency.

The as const on requires is what makes a dependency's members show up on the composed instance's type. Without it, requires = [HasAttributes] still composes and works fine at runtime — TS just widens the array to Constructor[], losing which constructor is in it, so it can't add attributes to Model's type. as const keeps it a literal tuple instead.

Conflicts

If two traits (or a trait and the class itself) define the same property or method name, composition throws instead of letting one silently clobber the other:

class A { value = 1 }
class B { value = 2 }

Compose.with(A, B) // fine, conflict isn't checked until instantiation for
                    // instance fields; prototype method conflicts throw here
new (Compose.with(A, B))()
// Error: Composition conflict: "value" from B already exists.

Testing

bun test
bun test --coverage