Advertisement
Guest User

Terrifying TS Types

a guest
Oct 9th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1.  
  2. export type TRpcHandlers = {
  3. listDecks: (data: ListDecksRequest) => Promise<ListDecksResponse>;
  4. getDeckById: (data: GetDeckByIdRequest) => Promise<GetDeckByIdResponse>;
  5. scryfallBulkDataLoader: (data: object) => Promise<string[]>;
  6. createDeck: (data: CreateDeckRequest) => Promise<CreateDeckResponse>;
  7. };
  8.  
  9. export type TRpcEvent = {
  10. reply: (channel: string, ...args: any[]) => void;
  11. };
  12.  
  13. export type TRpcMain = {
  14. on: (
  15. channel: string,
  16. handler: (event: TRpcEvent, request: RpcRequest<any>) => void
  17. ) => void;
  18. };
  19.  
  20. export type TRpcServer = {
  21. [key in keyof TRpcHandlers]: (
  22. event: TRpcEvent,
  23. data: RpcRequest<ArgumentTypes<TRpcHandlers[key]>[0]>
  24. ) => Promise<RPCResponseType<TRpcHandlers[key]>>;
  25. };
  26.  
  27. export type ArgumentTypes<F extends Function> = F extends (
  28. ...args: infer A
  29. ) => any
  30. ? A
  31. : never;
  32.  
  33. export type RPCResponseType<F extends Function> = F extends (
  34. ...args: any[]
  35. ) => Promise<infer R>
  36. ? R
  37. : never;
  38.  
  39.  
  40. export type RpcRequestType<R> = R extends RpcRequest<infer T> ? T : never;
  41.  
  42. export type TRpcClient = {
  43. [key in keyof TRpcServer]: (
  44. data: RpcRequestType<ArgumentTypes<TRpcServer[key]>[1]>
  45. ) => ReturnType<TRpcServer[key]>;
  46. };
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement