SHARE
TWEET

Untitled

a guest Jun 27th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type MessageDefs = {
  2.  
  3.   // This whole object will be passed as the first argument of the socket.io packet
  4.   'user.get': {
  5.     id: string;
  6.  
  7.     // Except for the optional `ack`, which itself represents the first argument of the acknowledgment
  8.     ack: User;
  9.   }
  10.  
  11. }
  12.  
  13. export default MessageDefs;
  14.  
  15. type Omit<T, K extends string> = Pick<T, Exclude<keyof T, K>>;
  16.  
  17. type Listener<M extends keyof MessageDefs> = (param: Omit<MessageDefs[M], 'ack'>, ack: Ack<M>) => void;
  18. type Ack<M extends keyof MessageDefs> = MessageDefs[M] extends { ack: infer P } ? ((param: P) => void) : never;
  19.  
  20. export type Socket<Base> = Omit<Base, 'on' | 'emit'> & {
  21.     on<M extends keyof MessageDefs>(event: M, listener: Listener<M>): void;
  22.     on<M extends keyof MessageDefs>(event: M, ...listeners: Listener<M>[]): void;
  23.     emit<M extends keyof MessageDefs>(event: M, param: Omit<MessageDefs[M], 'ack'>, ack?: Ack<M>): void;
  24. };
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