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

typed-event-types

v1.0.1

Published

This package provides TypeScript types that help you add typed events to your `EventTarget` subclasses.

Downloads

4

Readme

This package provides TypeScript types that help you add typed events to your EventTarget subclasses.

Usage

To create a subclass of EventTarget that has custom events:

import type { AddEvents } from 'typed-event-types';

interface WhateverEventMap {
  whatever: CustomEvent<number>;
}

class ObjectWithEvents extends (EventTarget as AddEvents<typeof EventTarget, WhateverEventMap>) {}
const o = new ObjectWithEvents();

o.addEventListener('whatever', (e) => {
  console.info(e.detail);  // <-- TS now knows this is a number
});

This also works for Custom Elements (or anything else that already has events), and keeps all the existing events:

class ElementWithMoreEvents extends (HTMLElement as AddEvents<typeof HTMLElement, WhateverEventMap>) {}
const e = new ElementWithMoreEvents();

e.addEventListener('whatever', (e) => {
  console.info(e.detail);  // <-- TS now knows this is a number
});
e.addEventListener('click', (e) => {
  console.info(e.movementX);  // <-- TS still knows this is a MouseEvent
});

Great!

You can also extend your types again, because that's the point:

class ExtendedEvenMoreEvents extends (ElementWithMoreEvents as AddEvents<typeof ElementWithMoreEvents, {
  anotherEvent: CustomEvent<string>;
}>) {}
const ee = new ExtendedEvenMoreEvents();
ee.addEventListener('anotherEvent', (e) => {
  console.info(e.detail);  // <-- TS now knows this is a string
});
ee.addEventListener('whatever', (e) => {
  console.info(e.detail);  // <-- TS still knows this is a number
});

Background

This has historically been a hard problem.

TypeScript internally solves this by adding/replacing the addEventListener call on all its internal interfaces whenever it needs to add more types. But this isn't additive, you need to replace all the events every time.

For example, on the media elements, TypeScript does this:

interface HTMLMediaElementEventMap extends HTMLElementEventMap {
    "encrypted": MediaEncryptedEvent;
    "waitingforkey": Event;
}
interface HTMLMediaElement extends HTMLElement {
    addEventListener<K extends keyof HTMLMediaElementEventMap>(type: K, listener: (this: HTMLMediaElement, ev: HTMLMediaElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
}

…which is fine, but annoying for developers to do every time: you can't extend something easily as you need to know what set of events your parent had.

The approach in this package solves this by adding specific addEventListener etc calls which accept a type literal, which TypeScript happily hoists and merges with the other keys. (TypeScript could use this approach internally, too—it would be a bit more verbose in the generated types, but make the built-in types much easier to write.)

So how does this work? Well… check out the source.

Attributions

This internally includes UnionToIntersection from here. Thanks, @ddprrt.

Contributions

The syntax is pretty verbose. I've tried to make it slightly nicer to work with a builder function—it doesn't do anything, just returns the argument but casts it—but it ends up being a bit unsafe.

Want to contribute? @-me or you know, do GitHub things.

Usage

Install via "typed-event-types".

But! If you want to take this code and include it in a TS helper library—I'd love that! It is a bit niche, as it specifically targets the DOM. Just attribute me (Apache-2.0) or tell me where to contribute the PR.