
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
@livekit/typed-emitter
Advanced tools
Strictly typed event emitter interface for TypeScript 3. Original Implementation by Andy Wermke (https://github.com/andywer)
[!NOTE] This is LiveKit's fork of the dormant
typed-emitter
library, which fixes issues with newer ECMAScript targets. It is licensed under the original MIT license, but support will not be provided and pull requests will be closed. Use at your own risk.
Strictly typed event emitter interface for TypeScript.
Code size: Zero bytes - Just the typings, no implementation. Use the default event emitter of the events
module in node.js or bring your favorite implementation when writing code for the browser.
$ npm install --save-dev typed-emitter
# Using yarn:
$ yarn add --dev typed-emitter
import EventEmitter from "events"
import TypedEmitter from "typed-emitter"
// Define your emitter's types like that:
// Key: Event name; Value: Listener function signature
type MessageEvents = {
error: (error: Error) => void,
message: (body: string, from: string) => void
}
const messageEmitter = new EventEmitter() as TypedEmitter<MessageEvents>
// Good 👍
messageEmitter.emit("message", "Hi there!", "no-reply@test.com")
// TypeScript will catch those mistakes ✋
messageEmitter.emit("mail", "Hi there!", "no-reply@test.com")
messageEmitter.emit("message", "Hi there!", true)
// Good 👍
messageEmitter.on("error", (error: Error) => { /* ... */ })
// TypeScript will catch those mistakes ✋
messageEmitter.on("error", (error: string) => { /* ... */ })
messageEmitter.on("failure", (error: Error) => { /* ... */ })
You might find yourself in a situation where you need to extend an event emitter, but also want to strictly type its events. Here is how to.
class MyEventEmitter extends (EventEmitter as new () => TypedEmitter<MyEvents>) {
// ...
}
As a generic class:
class MyEventEmitter<T> extends (EventEmitter as { new<T>(): TypedEmitter<T> })<T> {
// ...
}
fromEvent
types inferenceThe default fromEvent
from RxJS will return an Observable<unknown>
for our typed emitter.
This can be fixed by the following code, by replacing the fromEvent
type with our enhanced one: FromEvent
:
import { fromEvent as rxFromEvent } from "rxjs"
import { default as TypedEmitter, FromEvent } from "typed-emitter/rxjs"
// The `Observable` typing can be correctly inferenced
const fromEvent = rxFromEvent as FromEvent
Learn more from rxjs fromEvent compatibility #9
for the fromEvent
compatibility discussions.
The interface that comes with @types/node
is not type-safe at all. It does not even offer a way of specifying the events that the emitter will emit...
The eventemitter3
package is a popular event emitter implementation that comes with TypeScript types out of the box. Unfortunately there is no way to declare the event arguments that the listeners have to expect.
There were a few other examples of type-safe event emitter interfaces out there as well. They were either not published to npm, had an inconsistent interface or other limitations.
MIT
FAQs
Strictly typed event emitter interface for TypeScript 3. Original Implementation by Andy Wermke (https://github.com/andywer)
The npm package @livekit/typed-emitter receives a total of 8,888 weekly downloads. As such, @livekit/typed-emitter popularity was classified as popular.
We found that @livekit/typed-emitter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.