import { Observable } from "rxjs"; import type { Result } from "./result"; /** * Utility types to extract RPC request/response types from service interfaces. * This avoids manually maintaining discriminated unions for each RPC method. */ // Extract all method names from a service interface (excluding non-function properties) export type MethodNames = { [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never; }[keyof T] & string; // Extract request type from a method signature export type RequestType = T extends (req: infer R) => any ? R : never; // Extract response type from a method signature (handles both Promise and Observable) export type ResponseType = T extends (req: any) => Promise ? R : T extends (req: any) => Observable ? R : never; // Build discriminated union of { method, request } for all methods in a service export type RpcRequestFromService = { [K in MethodNames]: { method: K; request: RequestType }; }[MethodNames]; // Build discriminated union of { method, request, result } for all methods in a service export type RpcResultFromService = { [K in MethodNames]: { method: K; request: RequestType; result: Result, Error>; }; }[MethodNames];