Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update type definitions #219

Merged
merged 2 commits into from
May 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 72 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
type EventNames<T extends string | symbol | { [K in string | symbol]: any[] }> = T extends string | symbol ? T : keyof T;
type EventArgs<T extends string | symbol | { [K in string | symbol]: any[] }, K extends EventNames<T>> = T extends string | symbol ? any[] : K extends keyof T ? T[K] : never;
type ValidEventTypes<T = any> = string | symbol | T extends {
[K in keyof T]: any[] | ((...args: any[]) => void);
}
? T
: never;

type EventNames<T extends ValidEventTypes> = T extends string | symbol
? T
: keyof T;

type Handler<
T extends any[] | ((...args: any[]) => R),
R = any
> = T extends any[] ? (...args: T) => R : T;

type EventListener<
T extends ValidEventTypes,
K extends EventNames<T>
> = T extends string | symbol
? (...args: any[]) => void
: K extends keyof T
? Handler<T[K], void>
: never;

type EventArgs<T extends ValidEventTypes, K extends EventNames<T>> = Parameters<
EventListener<T, K>
>;

/**
* Minimal `EventEmitter` interface that is molded against the Node.js
* `EventEmitter` interface.
*/
declare class EventEmitter<EventTypes extends string | symbol | { [K in keyof EventTypes]: any[] } = string | symbol> {
declare class EventEmitter<
EventTypes extends
| string
| symbol
| {}
| { [K in keyof EventTypes]: any[] | ((...args: any[]) => void) } =
| string
| symbol,
Context extends any = any
> {
static prefixed: string | boolean;

/**
Expand All @@ -17,7 +51,9 @@ declare class EventEmitter<EventTypes extends string | symbol | { [K in keyof Ev
/**
* Return the listeners registered for a given event.
*/
listeners<T extends EventNames<EventTypes>>(event: T): Array<EventEmitter.ListenerFn<EventArgs<EventTypes, T>>>;
listeners<T extends EventNames<EventTypes>>(
event: T
): Array<EventListener<EventTypes, T>>;

/**
* Return the number of listeners listening to a given event.
Expand All @@ -27,24 +63,49 @@ declare class EventEmitter<EventTypes extends string | symbol | { [K in keyof Ev
/**
* Calls each of the listeners registered for a given event.
*/
emit<T extends EventNames<EventTypes>>(event: T, ...args: EventArgs<EventTypes, T>): boolean;
emit<T extends EventNames<EventTypes>>(
event: T,
...args: EventArgs<EventTypes, T>
): boolean;

/**
* Add a listener for a given event.
*/
on<T extends EventNames<EventTypes>>(event: T, fn: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any): this;
addListener<T extends EventNames<EventTypes>>(event: T, fn: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any): this;
on<T extends EventNames<EventTypes>>(
event: T,
fn: EventListener<EventTypes, T>,
context?: Context
): this;
addListener<T extends EventNames<EventTypes>>(
event: T,
fn: EventListener<EventTypes, T>,
context?: Context
): this;

/**
* Add a one-time listener for a given event.
*/
once<T extends EventNames<EventTypes>>(event: T, fn: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any): this;
once<T extends EventNames<EventTypes>>(
event: T,
fn: EventListener<EventTypes, T>,
context?: Context
): this;

/**
* Remove the listeners of a given event.
*/
removeListener<T extends EventNames<EventTypes>>(event: T, fn?: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any, once?: boolean): this;
off<T extends EventNames<EventTypes>>(event: T, fn?: EventEmitter.ListenerFn<EventArgs<EventTypes, T>>, context?: any, once?: boolean): this;
removeListener<T extends EventNames<EventTypes>>(
event: T,
fn?: EventListener<EventTypes, T>,
context?: Context,
once?: boolean
): this;
off<T extends EventNames<EventTypes>>(
event: T,
fn?: EventListener<EventTypes, T>,
context?: Context,
once?: boolean
): this;

/**
* Remove all listeners, or those of the specified event.
Expand All @@ -58,7 +119,7 @@ declare namespace EventEmitter {
}

export interface EventEmitterStatic {
new<EventTypes extends string | symbol | { [K in keyof EventTypes]: any[] } = string | symbol>(): EventEmitter<EventTypes>;
new <EventTypes extends ValidEventTypes>(): EventEmitter<EventTypes>;
}

export const EventEmitter: EventEmitterStatic;
Expand Down