typed-emitter
Advanced tools
Comparing version 1.5.0-from-event to 2.0.0
@@ -1,4 +0,4 @@ | ||
export type Arguments<T> = [T] extends [(...args: infer U) => any] | ||
? U | ||
: [T] extends [void] ? [] : [T] | ||
type EventMap = { | ||
[key: string]: (...args: any[]) => void | ||
} | ||
@@ -10,16 +10,14 @@ /** | ||
* | ||
* ```typescript | ||
* interface MyEvents { | ||
* error: (error: Error) => void | ||
* message: (from: string, content: string) => void | ||
* error: (error: Error) => void; | ||
* message: (from: string, content: string) => void; | ||
* } | ||
* | ||
* const myEmitter = new EventEmitter() as TypedEmitter<MyEvents> | ||
* const myEmitter = new EventEmitter() as TypedEmitter<MyEvents>; | ||
* | ||
* myEmitter.on("message", (from, content) => { | ||
* // ... | ||
* }) | ||
* | ||
* myEmitter.emit("error", "x") // <- Will catch this type error | ||
* myEmitter.emit("error", "x") // <- Will catch this type error; | ||
* ``` | ||
*/ | ||
interface TypedEventEmitter<Events> { | ||
interface TypedEventEmitter<Events extends EventMap> { | ||
addListener<E extends keyof Events> (event: E, listener: Events[E]): this | ||
@@ -35,7 +33,7 @@ on<E extends keyof Events> (event: E, listener: Events[E]): this | ||
emit<E extends keyof Events> (event: E, ...args: Arguments<Events[E]>): boolean | ||
emit<E extends keyof Events> (event: E, ...args: Parameters<Events[E]>): boolean | ||
// The sloppy `eventNames()` return type is to mitigate type incompatibilities - see #5 | ||
eventNames (): (keyof Events | string | symbol)[] | ||
rawListeners<E extends keyof Events> (event: E): Function[] | ||
listeners<E extends keyof Events> (event: E): Function[] | ||
rawListeners<E extends keyof Events> (event: E): Events[E][] | ||
listeners<E extends keyof Events> (event: E): Events[E][] | ||
listenerCount<E extends keyof Events> (event: E): number | ||
@@ -42,0 +40,0 @@ |
{ | ||
"name": "typed-emitter", | ||
"version": "1.5.0-from-event", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Strictly typed event emitter interface for TypeScript 3.", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
7143
67