
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
typescript-event-target
Advanced tools
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.
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.
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:
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.
Install the package:
npm i --save typescript-event-target
Then import as follows:
import { TypedEventTarget } from 'typescript-event-target';
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 importhttps://deno.land/x/typescript_event_target@v1.0.0/mod.ts
.
// 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
});
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() })
);
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) => {
/* [...] */
});
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
});
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 |
FAQs
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.
The npm package typescript-event-target receives a total of 93,723 weekly downloads. As such, typescript-event-target popularity was classified as popular.
We found that typescript-event-target demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.