crtxdmp.

A collection of ideas, snippets and other things.


Custom events for classes in typescript with tiny-typed-emitter

If you want to add events to your Typescript classes tiny-typed-emitter makes things a lot easier for you
without adding overhead.

Github: binier/tiny-typed-emitter

NPM: binier/tiny-typed-emitter

yarn add tiny-typed-emitter
import { TypedEmitter } from 'tiny-typed-emitter';

export enum EventName {
    ADDED = 'added'
}

interface MyClassEvents {
  'added': (thing: string, wasNew: boolean) => void;
}

class MyClass extends TypedEmitter<MyClassEvents> {

    constructor() {
        super();
    }
    
    public add(thing: string): void {
        this.emit(EventName.ADDED, thing, true);
    }
}

, , — Nov 8, 2021