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

traits-in-ts

v0.1.0

Published

PHP-inspired trait composition for TypeScript with proper inheritance and runtime instanceof support.

Readme

Traits in typescript

PHP-inspired trait composition for TypeScript with proper inheritance and runtime instanceof support.

traits-in-ts is a small experimental utility that explores how PHP-style traits can be implemented in TypeScript without decorators, transpilers, or mixin hacks.

It allows composing classes from multiple traits, preserves constructors, supports inheritance, and provides runtime instanceof Trait checks, while keeping strong TypeScript typing.

This project is primarily built for experimentation and learning. It can be used in real projects if you understand the trade-offs and limitations.

Features

  • Multiple traits per class
  • Trait constructors are executed
  • Works with existing class inheritance
  • Traits are inherited by subclasses
  • Runtime instanceof Trait support
  • Accurate TypeScript inference using intersection types

Installation

npm install traits-in-ts

or, if using pnpm:

pnpm add traits-in-ts

Basic usage

Define traits

class Trait1 {
  x = "trait1";

  hello() {
    console.log("hello from trait1");
  }
}

class Trait2 {
  hello(name: string) {
    console.log("hello from trait2", name);
  }
}

Compose them into a class

class A extends use([Trait1, Trait2]) {}

Use the class

const a = new A();

a.x;            // "trait1"
a.hello("TS");

a instanceof Trait1; // true
a instanceof Trait2; // true

Using traits with an existing parent class

If your class already extends a parent, you can keep it by passing the parent as the second argument to use.

class Parent {
  prop = "parent";
}

class A extends use([Trait1, Trait2], Parent) {}

This preserves normal class inheritance while adding traits.

const a = new A();

a.prop;         // "parent"
a.x;            // from Trait1

a instanceof Parent; // true
a instanceof Trait1; // true

Trait inheritance

Traits are inherited transitively through class inheritance.

class Trait3 {
  prop33 = "Hi";
}

class B extends use([Trait3], A) {}

const b = new B();

b instanceof Trait1; // true
b instanceof Trait3; // true

Traits applied to a parent class are automatically available on subclasses.

How it works (overview)

  • Each trait is instantiated and merged into the target instance
  • Trait methods are copied to the composed class prototype
  • Trait identity is stored as runtime metadata
  • instanceof Trait is implemented via Symbol.hasInstance
  • Trait metadata flows naturally through super() calls

Traits are treated as runtime composition metadata rather than inheritance.

Limitations and notes

  • This library is experimental and not production-hardened
  • Trait method conflicts are resolved using a “last trait wins” strategy
  • Trait constructors receive the same arguments as the class constructor
  • Symbol.hasInstance is modified on trait classes

Status

Experimental / educational.

The goal of this project is to explore trait semantics in TypeScript and understand the boundaries of the language.

License

Distributed under the MIT License. See LICENSE file for more details.

© 2025 Hichem Taboukouyout