Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typed-event-target

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-event-target

EventTarget in the browser but with strong event typing.

  • 4.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

typed-event-target

This package provides a strongly typed EventTarget sub class. Provided types pass down to event listeners and place requirements on event dispatching.

Usage

npm i typed-event-target

TypedEventTarget

Extend TypedEventTarget to create your own event target with specific types, or just use it to add types to an existing event target.

import {TypedEventTarget} from 'typed-event-target';
import {MyEvent1, MyEvent2} from './typed-events.example.js';

export class MyTypedEventTarget extends TypedEventTarget<MyEvent1 | MyEvent2> {}
import {TypedEventTarget} from 'typed-event-target';
import {MyEvent1, MyEvent2} from './typed-events.example.js';

export const nowWithTypes = new EventTarget() as TypedEventTarget<MyEvent1 | MyEvent2>;

In the end, nowWithTypes will be functionally equivalent to any new instance of MyEventTarget (new MyEventTarget()) because MyEventTarget is a sub-class of EventTarget with nothing extra but type information. (Though of course, nowWithTypes won't be an instance of MyEventTarget because MyEventTarget is a separate run-time class.)

Instances of TypedEventTarget can be used just like any instance of EventTarget:

import {MyTypedEventTarget} from './my-event-target.example.js';
import {MyEvent1} from './typed-events.example.js';

const myInstance = new MyTypedEventTarget();

function myListener(event: MyEvent1) {
    console.info(event);
}

myInstance.addEventListener(MyEvent1.type, myListener);
myInstance.dispatchEvent(new MyEvent1());
myInstance.removeEventListener(MyEvent1.type, myListener);

Typed Events

You are free to add the type property information to your Event sub-classes (for passing into TypedEventTarget's generic) however you wish, but this package provides an easy way to do so with defineTypedEvent:

import {defineTypedEvent} from 'typed-event-target';

export class MyEvent1 extends defineTypedEvent('my-event-type-1') {}
export class MyEvent2 extends defineTypedEvent('my-event-type-2') {}

Typed Custom Events

Typed custom events are also provided, so you can add type guards to the data included in your events. Note the extra () call after defineTypedCustomEvent. This is needed so you don't have to duplicate the Event type information as a function generic.

import {defineTypedCustomEvent} from 'typed-event-target';

export class MyEvent1 extends defineTypedCustomEvent<string>()('my-event-type-1') {}
export class MyEvent2 extends defineTypedCustomEvent<number>()('my-event-type-2') {}

console.info(new MyEvent1({detail: 'five'}));
console.info(new MyEvent2({detail: 5}));

Keywords

FAQs

Package last updated on 07 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc