Skip to content

Instantly share code, notes, and snippets.

@ChrisShank
Created June 20, 2025 12:06
Show Gist options
  • Save ChrisShank/97156c8027980b62a64939365b4c2c32 to your computer and use it in GitHub Desktop.
Save ChrisShank/97156c8027980b62a64939365b4c2c32 to your computer and use it in GitHub Desktop.
How I define custom events
import {MyEvent} from './my-event.ts';
document.addEventListener(MyEvent.name, (e) => {...});
export class MyEvent extends Event {
static readonly name = 'my-event';
#data;
get data() { return this.#data }
constructor(data: MyData, options: EventInit = {}) {
super(MyEvent.name, {bubbles: true, ...options});
this.#data = data;
}
}
declare global {
interface ElementEventMap {
[MyEvent.name]: MyEvent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment