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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mixin-decorators

v1.0.1

Published

Mixin classes via decorators

Downloads

14

Readme

Mixin classes via decorators

TypeScript 5.0 introduces us to ECMAScript decorators, which are quite different from the experimental decorators of the past. With a little bit of wiring, and some very specific constraints, we can build a new kind of mix-in class.

MultiMixinBuilder and its helper types (SubclassDecorator, StaticAndInstance most notably) provide everything you need to build out your mix-in. The main benefit of MultiMixinBuilder is it returns a Class with an aggregate type, combining all the static and instance fields you defined. Built-in TypeScript 5 decorators don't provide the aggregate type.

Example

import MultiMixinBuilder, {
  type StaticAndInstance,
  type SubclassDecorator,
} from "mixin-decorators";

class MixinBase {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
  constructor(...args: any[])
  {
    // do nothing
  }
}

// #region XVector
declare const XVectorKey: unique symbol;

interface XVector extends StaticAndInstance<typeof XVectorKey> {
  staticFields: {
    xCoord: number;
  }
  instanceFields: {
    get xLength(): number;
    set xLength(value: number);
  }
  symbolKey: typeof XVectorKey;
}

const Mixin_XVector: SubclassDecorator<XVector, typeof MixinBase, false> = function(
  this: void,
  _class: typeof MixinBase,
  context: ClassDecoratorContext<typeof MixinBase>,
)
{
  if (context.kind !== "class") {
    throw new Error("what's happening?")
  }

  return class extends _class {
    static xCoord = 12;
    xLength = 0;

    constructor(...args: unknown[]) {
      super(...args);
    }
  }
}
// #endregion XVector

// #region YVector
declare const YVectorKey: unique symbol;

interface YVector extends StaticAndInstance<typeof YVectorKey> {
  staticFields: {
    yCoord: number;
  }
  instanceFields: {
    yLength: number;
  }
  symbolKey: typeof YVectorKey;
}

const Mixin_YVector: SubclassDecorator<YVector, typeof MixinBase, [number]> = function(
  yCoordStatic: number
)
{
  return function(
    this: void,
    _class: typeof MixinBase,
  )
  {
    return class extends _class {
      static yCoord = yCoordStatic;
      yLength = 4;
    }
  }
}
// #endregion YVector

/*
const XYVector =
@Mixin_XVector
@Mixin_YVector(7)
class extends MixinBase {
};
*/

const XYVector = MultiMixinBuilder<[
  XVector, YVector
], typeof MixinBase>
(
  [
    Mixin_XVector, Mixin_YVector(7)
  ], MixinBase
);

const xy = new XYVector;

it("xy", () => {
  expect(xy.xLength).toBe(0);
  expect(xy.yLength).toBe(4);
});

it("XYVector", () => {
  expect(XYVector.xCoord).toBe(12);
  expect(XYVector.yCoord).toBe(7);
});

Without MultiMixinBuilder. the xLength and yLength properties of xy would be unknown to TypeScript. Likewise, TypeScript wouldn't know about XYVector.xCoord or XYVector.yCoord.

Installation

npm install --save-dev mixin-decorators

Under the hood

Type definitions

  1. A class decorator type which is aware of the new context argument. TypeScript 5.0's built-in ClassDecorator type won't work.. ClassDecoratorFunction fills the bill.
  2. Classes have static fields, which means a special type to define the static and instance fields of a subclass. StaticAndInstance defines this.
  3. Without depending on StaticAndInstance, we need a type to define how a mix-in class joins the base class and its subclass's static and instance fields. MixinClass is a little convoluted, but works well.
  4. Combining a base class with StaticAndInstance and ClassDecoratorFunction offers a SubclassDecorator type. An array of StaticAndInstance types gives rise to a SubclassDecoratorSequence type in the same file.
  5. MultiMixinClass defines a MixinClass type from an array of StaticAndInstance objects.

How do I use these types?

Classes

  • MultiMixinBuilder combines all of the above:
    • It takes a type parameter, Interfaces, which is an ordered array of StaticAndInstance types.
    • It takes a parameter, decorators, which is a SubclassDecoratorSequence mapping the Interfaces to SubclassDecorator instances.
    • It also takes a parameter, baseClass, which must be an subclass of MixinBase.
    • It returns a MultiMixinClass from the base class and invoking all the SubclassDecorator functions.

Rules to follow

  • The ordering of decorators in MultiMixinBuilder determines the chain of derived-to-base classes.