/** * 在 TypeScript 里实现通用的柯里化函数是一件比较困难的事情。 * * 以下是一些讨论: * * - [Generic curry function with TypeScript 3 - stack overflow](https://stackoverflow.com/questions/51859461/generic-curry-function-with-typescript-3) * - [TypeScript and currying - GitHub Gist](https://gist.github.com/donnut/fd56232da58d25ceecf1) * * 这是目前为止我找到的最好的一个解决方案,我大幅度借鉴了这个方案,感谢 Mills: * * - [How to master advanced TypeScript patterns - Pierre-Antoine Mills](https://www.freecodecamp.org/news/typescript-curry-ramda-types-f747e99744ab/) * - [Related Repo](https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts) * * 这个也不错: * * - [Currying in TypeScript - Jamie Pennell](https://medium.com/codex/currying-in-typescript-ca5226c85b85) * * 这是相关的 PR、Proposal、Updates: * * - [[Ramda] Generic types for curry #33628](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33628) * - [Proposal: Variadic Kinds -- Give specific types to variadic functions #5453](https://github.com/Microsoft/TypeScript/issues/5453) * - [Variadic tuple types #39094](https://github.com/microsoft/TypeScript/pull/39094) * - [Variadic Tuple Types - TypeScript 4.0](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#variadic-tuple-types) */ import { asIs } from '../internal/base'; interface ArgPlaceholder { '@@functional/placeholder': true; isArgPlaceholder: true; } export declare const argPlaceholder: ArgPlaceholder; /** * 判断一个参数是否是 ArgPlaceHolder */ export declare const isArgPlaceholder: (tar: any) => tar is ArgPlaceholder; /****************************************************************************************************** * * Simple Curry Function * ******************************************************************************************************/ declare type Loose = [...T, ...any[]]; declare type PartialTuple = TUPLE extends [infer NEXT_PARAM, ...infer REMAINING] ? PartialTuple : [ ...EXTRACTED, ...TUPLE ]; declare type NonNull = { [K in keyof T]: NonNullable; }; declare type PartialParameters any> = NonNull>>; declare type RemainingParameters = EXPECTED extends [infer E1, ...infer EX] ? (PROVIDED extends [infer P1, ...infer PX] ? (P1 extends E1 ? RemainingParameters : never) : (LOOSE extends true ? Loose : EXPECTED)) : (LOOSE extends true ? Loose<[]> : []); declare type SimpleCurriedFunctionOrReturnValue any, LOOSE extends boolean = false> = RemainingParameters, LOOSE> extends [any, ...any[]] ? SimpleCurriedFunction : ReturnType; declare type SimpleCurriedFunction any, LOOSE extends boolean = false> = , LOOSE>>>>(...args: APPENDING_ARGS) => SimpleCurriedFunctionOrReturnValue<[...PROVIDED, ...APPENDING_ARGS], FN, LOOSE>; declare type LooseCurryS = any, EXISTING_ARGS extends [...PartialParameters, ...any[]]>(targetFn: FN, ...existingArgs: EXISTING_ARGS) => SimpleCurriedFunction; /** * Simple curry function - loose version * * Loose simple curry will pass all of the args it received to target function, * even if the arg's length is greater than target function's arity. */ export declare const looseCurryS: LooseCurryS; declare type CurryS = any, EXISTING_ARGS extends PartialParameters>(targetFn: FN, ...existingArgs: EXISTING_ARGS) => SimpleCurriedFunction; /** * Simple curry function - strict version * * Strict simple curry will take exactly the required args that target function needs, * redundant args will be ignored. */ export declare const curryS: CurryS; declare type Tail = ((...t: T) => any) extends ((_: any, ...tail: infer TT) => any) ? TT : []; declare type Length = T['length']; declare type Prepend = [E, ...T]; declare type Drop = { 0: Drop, Prepend>; 1: T; }[Length extends N ? 1 : 0]; declare type Cast = X extends Y ? X : Y; declare type Pos = Length; declare type Next = Prepend; declare type Reverse = { 0: Reverse], R>, Next>; 1: R; }[Pos extends Length ? 1 : 0]; declare type Concat = Reverse extends infer R ? Cast : never, T2>; declare type LoosePlaceholder = [] & { __TYPE__: 'LoosePlaceholder'; }; declare type GapOf = T1[Pos] extends ArgPlaceholder ? (LOOSE extends true ? (T2[Pos] extends undefined ? [...TN, LoosePlaceholder] : [...TN, T2[Pos]]) : (T2[Pos] extends undefined ? TN : [...TN, T2[Pos]])) : TN; declare type GapsOf = { 0: GapsOf, any[]>, Next>; 1: Concat, T2> extends infer D ? Cast : never>; }[Pos extends Length ? 1 : 0]; declare type ConcatGaps = { 0: ConcatGaps, T1[0] extends ArgPlaceholder ? (Length extends 0 ? [] : Tail) : T2, LOOSE, T1[0] extends ArgPlaceholder ? (Length extends 0 ? [...TN, T1[0]] : [...TN, T2[0]]) : [...TN, T1[0]]>; 1: LOOSE extends true ? [...TN, ...T2] : TN; }[Length extends 0 ? 1 : 0]; declare type PartialGaps = { [K in keyof T]?: T[K] extends LoosePlaceholder ? any : (T[K] | ArgPlaceholder); }; declare type CleanedGaps = { [K in keyof T]: NonNullable; }; declare type Gaps = CleanedGaps>; declare type HPLooseCurry = (fn: AnyFunction, ...args: any[]) => AnyFunction; /** * Internal curry function - loose version * * Loose internal curry will pass all of the args it received to target function, * even if the arg's length is greater than target function's arity. */ export declare const looseCurry: HPLooseCurry; declare type HPCurry = (fn: AnyFunction, ...args: any[]) => AnyFunction; /** * Internal curry function - strict version * * Strict internal curry will take exactly the required args that target function needs, * redundant args will be ignored. */ export declare const curry: HPCurry; export declare type CurriedNFunctionOrReturnValue = GapsOf extends [any, ...any[]] ? CurriedNFunction : RETURN_TYPE; export declare type CurriedNFunction = extends infer __ ? (LOOSE extends true ? Gaps>> : Gaps>) : never)>(...args: APPENDING_ARGS) => ConcatGaps extends infer ___ ? CurriedNFunctionOrReturnValue, LOOSE> : never; declare type HPLooseCurryN = (targetArity: number, fn: AnyFunction, ...args: any[]) => AnyFunction; /** * Internal curryN function - loose version * * Loose internal curry will pass all of the args it received to target function, * even if the arg's length is greater than specified arity. */ export declare const looseCurryN: HPLooseCurryN; declare type HPCurryN = (targetArity: number, fn: AnyFunction, ...args: any[]) => AnyFunction; /** * Internal curryN function - strict version * * Strict internal curry will take exactly the required arity that specified, * redundant args will be ignored. */ export declare const curryN: HPCurryN; /****************************************************************************************************** * * Compose and Pipe Function * ******************************************************************************************************/ declare type AnyFunction = (...args: any[]) => any; export declare const composeL: HPPipe; export declare const composeR: HPCompose; export declare const pipeL: HPPipe; export declare const pipeR: HPCompose; declare type NAryF = (...args: S) => R; declare type UnaryF = (input: I) => O; interface HPPipe { (): typeof asIs; (fn1: NAryF): NAryF; (fn1: NAryF, fn2: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF, fn6: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF, fn6: UnaryF, fn7: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF, fn6: UnaryF, fn7: UnaryF, fn8: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF, fn6: UnaryF, fn7: UnaryF, fn8: UnaryF, fn9: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF, fn6: UnaryF, fn7: UnaryF, fn8: UnaryF, fn9: UnaryF, fn10: UnaryF): NAryF; (fn1: NAryF, fn2: UnaryF, fn3: UnaryF, fn4: UnaryF, fn5: UnaryF, fn6: UnaryF, fn7: UnaryF, fn8: UnaryF, fn9: UnaryF, fn10: UnaryF, ...fns: Array>): NAryF; } interface HPCompose { (): typeof asIs; (fn1: NAryF): NAryF; (fn2: UnaryF, fn1: NAryF): NAryF; (fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn5: UnaryF, fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn6: UnaryF, fn5: UnaryF, fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn7: UnaryF, fn6: UnaryF, fn5: UnaryF, fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn8: UnaryF, fn7: UnaryF, fn6: UnaryF, fn5: UnaryF, fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn9: UnaryF, fn8: UnaryF, fn7: UnaryF, fn6: UnaryF, fn5: UnaryF, fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; (fn10: UnaryF, fn9: UnaryF, fn8: UnaryF, fn7: UnaryF, fn6: UnaryF, fn5: UnaryF, fn4: UnaryF, fn3: UnaryF, fn2: UnaryF, fn1: NAryF): NAryF; } export declare const compose: HPCompose; export declare const pipe: HPPipe; /****************************************************************************************************** * * Memorize Function * ******************************************************************************************************/ declare type Memorize = (fn: FN, hasher: (...args: Parameters) => any) => (...args: Parameters) => ReturnType; export declare const defaultMemorizeHasher: (...args: any[]) => string; /** * Return a new function which bind to a cache map. * When the function is called, it will first check the cache map, if the result is found, return it. * Otherwise, call the function and cache the result. */ export declare const memorize: Memorize; export declare const invoker: (arity: number, key: string) => AnyFunction; export declare const looseInvoker: (arity: number, key: string) => AnyFunction; /****************************************************************************************************** * * Arity Function * ******************************************************************************************************/ declare type FallbackType = (fn: AnyFunction) => AnyFunction; export declare const nAry: AnyFunction; export declare const looseNAry: AnyFunction; export declare const quaternary: FallbackType; export declare const looseQuaternary: FallbackType; export declare const ternary: FallbackType; export declare const looseTernary: FallbackType; export declare const binary: FallbackType; export declare const looseBinary: FallbackType; export declare const unary: FallbackType; export declare const looseUnary: FallbackType; declare type Tap = (fn: AnyFunction) => (...args: [...any[], T]) => T; export declare const tap: Tap; declare type Execute = (fn: FN, ...args: [...Parameters, ...any[]]) => ReturnType; export declare const execute: Execute; export {}; /****************************************************************************************************** * * Quick Tests * ******************************************************************************************************/ //# sourceMappingURL=helpers.d.ts.map