SHARE
TWEET

Untitled

a guest Apr 26th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. declare class NodeEventEmitter<Target extends any = {}, Events = {}, EventNames extends keyof Events = keyof Events> {
  2.   addListener<K extends EventNames>(event: K, listener: (...args: InferArgs<Events[K]>) => void, ...targs: ArgsTail2<Target["addListener"]>): this;
  3.  
  4.   on<K extends EventNames>(event: K, listener: (...args: InferArgs<Events[K]>) => void, ...targs: ArgsTail2<Target["on"]>): this;
  5.  
  6.   once<K extends EventNames>(event: K, listener: (...args: InferArgs<Events[K]>) => void, ...targs: ArgsTail2<Target["once"]>): this;
  7.  
  8.   removeListener<K extends EventNames>(event: K, listener: (...args: any[]) => void, ...targs: ArgsTail2<Target["removeListener"]>): this;
  9.  
  10.   off<K extends EventNames>(event: K, listener: (...args: InferArgs<Events[K]>) => void, ...targs: ArgsTail2<Target["off"]>): this;
  11.  
  12.   removeAllListeners(event?: EventNames, ...targs: ArgsTail1<Target["removeAllListeners"]>): this;
  13.  
  14.   setMaxListeners(n: number, ...targs: ArgsTail1<Target["setMaxListeners"]>): this;
  15.  
  16.   getMaxListeners(...targs: ArgsTail0<Target["getMaxListeners"]>): number;
  17.  
  18.   listeners<K extends EventNames>(event: K, ...targs: ArgsTail1<Target["listeners"]>): Events[K];
  19.  
  20.   rawListeners<K extends EventNames>(event: K, ...targs: ArgsTail1<Target["rawListeners"]>): Events[K];
  21.  
  22.   emit<K extends EventNames>(event: K, ...args: InferArgs<Events[K]>): boolean;
  23.  
  24.   listenerCount(type: EventNames, ...targs: ArgsTail1<Target["listenerCount"]>): number;
  25.  
  26.   // Added in Node 6...
  27.   prependListener<K extends EventNames>(event: K, listener: (...args: InferArgs<Events[K]>) => void, ...targs: ArgsTail2<Target["prependListener"]>): this;
  28.  
  29.   prependOnceListener<K extends EventNames>(event: K, listener: (...args: InferArgs<Events[K]>) => void, ...targs: ArgsTail2<Target["prependOnceListener"]>): this;
  30.  
  31.   eventNames(...targs: ArgsTail0<Target["eventNames"]>): Array<EventNames>;
  32. }
  33.  
  34. export type AsTypeSafeEventEmitter<T, Events> =
  35.   Pick<NodeEventEmitter<T, Events>, Intersection<T, NodeEventEmitter>> &
  36.   Pick<T, Exclude<keyof T, Intersection<T, NodeEventEmitter>>>;
  37.  
  38. type ArgsTail0<T> = InferArgs<T>;
  39. type ArgsTail1<T> = T extends (a: any, ...args: infer U) => any ? U : never;
  40. type ArgsTail2<T> = T extends (a: any, b: any, ...args: infer U) => any ? U : never;
  41.  
  42. type InferArgs<T> = T extends (...args: infer U) => any ? U : never;
  43. type Intersection<A, B> = keyof A & keyof B;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top