Skip to content

Commit

Permalink
[ts] Refine types to improve extends behavior (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall authored Aug 27, 2020
1 parent 38de307 commit 8f8a7e0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ declare namespace EventEmitter {
>(): EventEmitter<EventTypes, Context>;
}

/**
* `object` should be in either of the following forms:
* ```
* interface EventTypes {
* 'event-with-parameters': any[]
* 'event-with-example-handler': (...args: any[]) => void
* }
* ```
*/
export type ValidEventTypes = string | symbol | object;

export type EventNames<T extends ValidEventTypes> = T extends string | symbol
Expand All @@ -105,17 +114,14 @@ declare namespace EventEmitter {
: any[];
};

export type Arguments<
T extends object,
K extends keyof T | string | symbol
> = K extends keyof T ? ArgumentMap<T>[K] : any[];

export type EventListener<
T extends ValidEventTypes,
K extends EventNames<T>
> = T extends string | symbol
? (...args: any[]) => void
: (...args: Arguments<Exclude<T, string | symbol>, K>) => void;
: (
...args: ArgumentMap<Exclude<T, string | symbol>>[Extract<K, keyof T>]
) => void;

export type EventArgs<
T extends ValidEventTypes,
Expand Down

0 comments on commit 8f8a7e0

Please sign in to comment.