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

typescript-event-target

v1.1.1

Published

Strictly typed EventTarget that directly extends EventTarget to function as a drop-in replacement. It works with all Event-Types and accounts for basically no additional bundle-size.

Downloads

91,058

Readme

TypedEventTarget NPM JSR Deno License

Strictly typed EventTarget that directly extends EventTarget to function as a drop-in replacement. It works with all Event-Types and accounts for basically no additional bundle-size.

Motivation

Since EventTarget support landed in NodeJS v14.5, they are the only way to go forward, when talking about event driven JS.
But EventTarget lacks in terms of developer experience and Typescript integration. To be specific:

  • No strictly typed event listeners & events
  • Missing proper IntelliSense integration
  • No auto-complete for event types

The weird thing is, that with JS-native objects, which implement EventTarget (like WebSocket, Worker or any HTML-Elements), you get all those features out of the box:

This package aims to fix these shortcomings and add all these missing features for custom EventTargets.

Installation

NPM

Install the package:

npm i --save typescript-event-target

Then import as follows:

import { TypedEventTarget } from 'typescript-event-target';

Deno

Either install from JSR:

deno add @derzade/typescript-event-target

or import directly form deno.land/x/:

import { TypedEventTarget } from 'https://deno.land/x/typescript_event_target/mod.ts';

:warning: Warning: It is best practice to "pin" to a particular version. https://deno.land/x/ supports using git tags in the URL to direct you at a particular version. So to use version 1.0.0 of TypedEventTarget, you would want to import https://deno.land/x/[email protected]/mod.ts.

Usage

  1. Basic Example
  2. Dispatching Events
  3. Extending TypedEventTarget
  4. Different Event Types

Basic Example

// Step 1: Create an interface, which
// includes all dispatchable events
interface MyEventMap {
    hello: Event;
    time: CustomEvent<number>;
}

// Step 2: Create your TypedEventTarget, with
// the EventMap as the type parameter
const eventTarget = new TypedEventTarget<MyEventMap>();

// Step 3: Strictly typed EventListeners 🎉
eventTarget.addEventListener('time', (event) => {
    // event is of type CustomEvent<number>

    const time = event.detail;

    // time is of type number
});

Dispatching Events

TypedEventTarget directly extends EventTarget, so dispatchEvent works as expected, but is marked as deprecated. The reason for this is that dispatchEvent cannot be strictly typed easily. Instead, TypedEventTarget introduces a dispatchTypedEvent method, which is strictly typed by taking an additional _type parameter (just used for type checking).

interface MyEventMap {
    time: CustomEvent<number>;
}

const eventTarget = new TypedEventTarget<MyEventMap>();

eventTarget.dispatchTypedEvent(
    'time',
    new CustomEvent('time', { detail: Date.now() })
);

Extending TypedEventTarget

Instead of directly instantiating TypedEventTarget, you can also extend it:

interface MyEventMap {
    time: CustomEvent<number>;
    // [...]
}

class MyEventTarget extends TypedEventTarget<MyEventMap> {
    /* [...] */
}

const myTarget = new MyEventTarget();
myTarget.addEventListener('time', (e) => {
    /* [...] */
});

Different Event Types

Your EventMap can include Event as well as any type, that extends Event. These can be native Events or even own classes:

class MyEvent extends Event {
    /* [...] */
}

class MyEventMap {
    boring: Event;
    custom: CustomEvent<number>;
    mine: MyEvent;
    mouse: MouseEvent;
    keyboard: KeyboardEvent;
}

const eventTarget = new TypedEventTarget<MyEventMap>();

eventTarget.addEventListener('mine', (e) => {
    // e is of type MyEvent
});

Bundle Size

This package mostly only contains TypeScript definitions. Therefore, it amounts up to basically no bundle size. The only thing that is bundled is the dispatchTypedEvent-method, which is just a simple wrapper around the native dispatchEvent-method.

| | Deflate | Brotli | Gzip | Uncompressed | | --------- | -----------: | ------------: | --------: | -----------: | | ES Module | 92 Bytes | 95 Bytes | 110 Bytes | 119 Bytes | | Common JS | 336 Bytes | 308 Bytes | 354 Bytes | 599 Bytes |