UNPKG

r19

Version:

Simple remote procedure calls in TypeScript

20 lines (19 loc) 2 kB
type AppendDotPathSegment<TDotPath extends string, TSegment extends string> = TDotPath extends "" ? TSegment : `${TDotPath}.${TSegment}`; type AnyFunction = (...args: any[]) => any; type Primitives = null | undefined | string | number | boolean | symbol | bigint | AnyFunction; type AllObjDotPaths<TObj, TDotPath extends string = ""> = TObj extends Primitives | unknown[] ? TDotPath : { [P in keyof TObj]: P extends string ? AppendDotPathSegment<TDotPath, P> | AllObjDotPaths<TObj[P], AppendDotPathSegment<TDotPath, P>> : TDotPath; }[keyof TObj]; type RecursiveOmitNested<TObj, TOmitPath extends string, TDotPath extends string = ""> = TObj extends Primitives | unknown[] ? TObj : { [P in keyof TObj as P extends string ? AppendDotPathSegment<TDotPath, P> extends TOmitPath ? never : P : never]: P extends string ? RecursiveOmitNested<TObj[P], TOmitPath, AppendDotPathSegment<TDotPath, P>> : TObj[P]; }; type OnlyProcedures<TProceduresInstance> = TProceduresInstance extends AnyFunction ? TProceduresInstance : { [P in keyof TProceduresInstance as OnlyProcedures<TProceduresInstance[P]> extends Exclude<Primitives, AnyFunction> | Record<PropertyKey, never> ? never : P]: OnlyProcedures<TProceduresInstance[P]>; }; export type OmittableProcedures<TProceduresInstance> = AllObjDotPaths<OnlyProcedures<TProceduresInstance>>; export type ProceduresFromInstance<TProceduresInstance, TOmitPaths extends string = never> = RecursiveOmitNested<OnlyProcedures<TProceduresInstance>, TOmitPaths>; type ProceduresFromInstanceConfig<TProceduresInstance, TUnknownOmitPaths extends string> = { omit?: readonly (TUnknownOmitPaths | OmittableProcedures<TProceduresInstance>)[]; }; export declare const proceduresFromInstance: <TProceduresInstance, TUnknownOmitPaths extends string = never>(proceduresInstance: TProceduresInstance, config?: ProceduresFromInstanceConfig<TProceduresInstance, TUnknownOmitPaths>) => RecursiveOmitNested<OnlyProcedures<TProceduresInstance>, TUnknownOmitPaths, "">; export {};