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

@lsagetlethias/tstrait

v2.0.2

Published

Trait is better than mixin

Downloads

53

Readme

@lsagetlethias/tstrait

Yet another mixin library but without mixins.

Goal

Depending on how your architecture or your codebase are made, sometimes Mixin are not good enough or simply not good at all. The Trait feature from PHP resolve the instance imutability that Mixin most of the times lacks of.

You can check some definition from PHP manual if you need to: https://www.php.net/manual/fr/language.oop5.traits.php

But TL;DR, Trait are made for multiple inheritance without constructor collision. In another words, traits gives you a way to factorize common methods between classes without putting them in a abstract class.
They can be usefull in override situations.

Installation

Node (or front)

yarn add @lsagetlethias/tstrait
# or
npm install @lsagetlethias/tstrait

Deno

// /!\ Not registered yet in the global deno registry /!\
// in you deps.ts
export * as tstait from 'https://raw.githubusercontent.com/lsagetlethias/tstrait/master/mod.ts';

// in your main.ts
import { tstrait } from './mod.ts';

const { Trait, Use } = tstrait;

Usage

Basic

At first, you need to create your trait. Becasue traits are not a language feature by default, you need to make you trait extends the Trait class to have some control in your types (in TypeScript only). Extending is not a problem because a trait cannot extends another class or implementing another interface. A trait can only use another trait.

class MyTrait extends Trait {
    // a trait doesn't need a constructor so you can skip it or even make your class abstract
    public something() {}
}

Then you will need the @Use decorator to apply your trait on your class. For the same reasons, the use keyword doesn't exists in JS/TS so an annotation should do the job instead.

@Use(MyTrait)
class Foo {}

const Bar = Use(MyTrait)(class Bar {}); // will be fully typed

At this point, any Foo instance will have a something() method for the trait used.

Regarding traits on Abstract Classes

Due to typings issues with abstract, abstract classes cannot be traited with decorators. Only straight call of Use function can works by emulating a fake public constructor on the abstract class with cast.

e.g.

import { Use, Ctor } from '@lsagetlethias/tstrait';
class MyTrait {}

@Use(MyTrait) // Argument of type 'typeof Foo' is not assignable to parameter of type 'Ctor'. Cannot assign an abstract constructor type to a non-abstract constructor type. ts(2345)
abstract class Foo {}

abstract class _Bar {}
const Bar = Use(MyTrait)(_Bar as Ctor<_Bar>); // ok

Advanced

If you need to, you can use as many trait as you want. In this case, the @Use will takes an array of trait as single parameter:

@Use(Trait1, Trait2, Trait3 /* ... and so on */)
class Foo {}

In this case, Traits will be applied in array order, from left to right.

Sometimes, you will also need to configure properly how your trait is applied or handle collision between multiple traits:

@Use(Trait1, Trait2, {
    as: { 'Trait1.method': 'fooBarFunction' } // The "method" from Trait1 will be aliased "fooBarFunction" before being applied to Foo class
})
class Foo {}

(new Foo()).method(); // KO ; doesn't exists on Foo
(new Foo()).fooBarFunction(); // ok

Config

As

With as, you can alias a member and/or change its scope (scopes are not yet handled).
Alias must be seen like this: alias "Trait.member" as "scope newName".

e.g.

@Use(Trait1, {
    as: { 'Trait1.method': 'fooBarFunction' } // Alias "Trait1.method" as "fooBarFunction" when used in Foo class
})
class Foo {}

(new Foo()).method(); // KO ; doesn't exists on Foo
(new Foo()).fooBarFunction(); // ok

InsteadOf

With insteadof, you can solve collision between similar Traits. InsteadOf must be seen like this: use "Trait1.method" instead of what's found in "[Trait2, Trait3, ...]" with the same method name

e.g.

class Trait1 extends Trait {
    public foo() {
        console.log('A');
    }
}
class Trait2 extends Trait {
    public foo() {
        console.log('B');
    }
}

@Use(Trait1, Trait2, {
    insteadOf: { 'Trait1.foo': [Trait2] } // Use Trait1.foo instead of the same method in Trait2
})
class Foo {}

(new Foo()).foo(); // will log "A" instead of "B"

Example

You can run the example with the command yarn start (or npm run start) See example file 😀

License

MIT