UNPKG

tiinvo

Version:

A library of types and utilities for your TypeScript and JavaScript projects

38 lines (37 loc) 1.81 kB
export type AsChain<f extends [FuncType, ...FuncType[]], g extends FuncType[] = Tail<f>> = { [k in keyof f]: (arg: ArgType<f[k]>) => ArgType<Lookup<g, k, any>, any>; }; export type AsAsyncChain<f extends [FuncType, ...FuncType[]], g extends FuncType[] = Tail<f>> = { [k in keyof f]: (arg: ArgType<f[k]>) => Promise<ArgType<Lookup<g, k, any>, any>>; }; export type ArgType<F, Else = never> = F extends (arg: infer A) => any ? A : Else; export type FuncType = (arg: any) => any; export type LastIndexOf<a extends any[]> = ((...x: a) => void) extends (y: any, ...z: infer b) => void ? b["length"] : never; export type Lookup<t, k extends keyof any, el = never> = k extends keyof t ? t[k] : el; export type Tail<a extends any[]> = ((...t: a) => void) extends (x: any, ...u: infer b) => void ? b : never; export type Pipe = <f extends [((arg: any) => any | (() => any)), ...Array<(arg: any) => any>]>(...f: f & AsChain<f>) => f[0] extends () => any ? () => ReturnType<f[LastIndexOf<f>]> : f[0] extends (arg: infer U) => any ? (arg: U) => ReturnType<f[LastIndexOf<f>]> : never; export type PipeAsync = <f extends [((arg: any) => Promise<any> | (() => Promise<any>)), ...Array<(arg: any) => Promise<any>>]>(...f: f & AsAsyncChain<f>) => f[0] extends () => any ? () => ReturnType<f[LastIndexOf<f>]> : f[0] extends (arg: infer U) => any ? (arg: U) => ReturnType<f[LastIndexOf<f>]> : never; /** * Same as the sync version, but handles promises. * * @since 4.0.0 */ export declare const async: PipeAsync; /** * Creates a pipeline of synchronous functions * * @example * * ```ts * import { Num, Pipe } from 'tiinvo' * * const vat = Pipe.sync(Num.div(100), Num.mul(22)); * * vat(100) // 22 * vat(150) // 33 * vat(200) // 44 * ``` * * @since 4.0.0 */ export declare const sync: Pipe;