UNPKG

2.87 kBTypeScriptView Raw
1import { Pos } from '../Iteration/Pos';
2import { Concat } from '../List/Concat';
3import { Length } from '../List/Length';
4import { Next } from '../Iteration/Next';
5import { Cast } from '../Any/Cast';
6import { Parameters } from './Parameters';
7import { Return } from './Return';
8import { IterationOf } from '../Iteration/IterationOf';
9import { Iteration } from '../Iteration/Iteration';
10import { NonNullableFlat } from '../Object/NonNullable';
11import { x } from '../Any/x';
12import { List } from '../List/List';
13import { Function } from './Function';
14import { Extends } from '../Any/Extends';
15import { Tail } from '../List/Tail';
16import { RequiredKeys } from '../List/RequiredKeys';
17/**
18 * @hidden
19 */
20declare type _SplitParams<P extends List, PSplit extends List[] = [], PRest extends List = Tail<P>> = {
21 0: P extends [...infer A, ...PRest] ? _SplitParams<Tail<P>, [...PSplit, A], Tail<PRest>> : never;
22 1: PSplit;
23 2: P[number][][];
24}[number extends Length<P> ? 2 : P extends [] ? 1 : 0];
25/**
26 * Splits tuples to preserve their labels
27 * @hidden
28 */
29declare type SplitParams<P extends List> = _SplitParams<P> extends infer X ? Cast<X, List[]> : never;
30/**
31 * @hidden
32 */
33declare type _JoinParams<PSplit extends List[], L extends List = []> = {
34 0: _JoinParams<Tail<PSplit>, [...L, ...PSplit[0]]>;
35 1: L;
36 2: PSplit[number][];
37}[number extends Length<PSplit> ? 2 : PSplit extends [] ? 1 : 0];
38/**
39 * Undoes the job of [[SplitParams]]
40 * @hidden
41 */
42declare type JoinParams<P extends List[]> = _JoinParams<P> extends infer X ? Cast<X, List> : never;
43/**
44 * @hidden
45 */
46declare type GapOf<L1 extends List, L2 extends List[], LN extends List, I extends Iteration> = L1[Pos<I>] extends x ? Concat<LN, L2[Pos<I>]> : LN;
47/**
48 * @hidden
49 */
50declare type _GapsOf<L1 extends List, L2 extends List[], LN extends List = [], L2D extends List[] = L2, I extends Iteration = IterationOf<0>> = {
51 0: _GapsOf<L1, L2, GapOf<L1, L2, LN, I>, Tail<L2D>, Next<I>>;
52 1: Concat<LN, JoinParams<L2D>>;
53}[Extends<Pos<I>, Length<L1>>];
54/**
55 * @hidden
56 */
57declare type GapsOf<L1 extends List, L2 extends List> = _GapsOf<L1, SplitParams<L2>> extends infer X ? Cast<X, List> : never;
58/**
59 * @hidden
60 */
61declare type Gaps<L extends List> = Cast<NonNullableFlat<{
62 [K in keyof L]?: L[K] | x;
63}>, List>;
64/**
65 * Curry a [[Function]]
66 * @param F to curry
67 * @returns [[Function]]
68 * @example
69 * ```ts
70 * import {F} from 'ts-toolbelt'
71 *
72 * /// If you are looking for creating types for `curry`
73 * /// It handles placeholders and variable arguments
74 * declare function curry<Fn extends F.Function>(fn: Fn): F.Curry<Fn>
75 * ```
76 */
77export declare type Curry<F extends Function> = <P extends Gaps<Parameters<F>>, G extends List = GapsOf<P, Parameters<F>>, R extends any = Return<F>>(...p: Gaps<Parameters<F>> | P) => RequiredKeys<G> extends never ? R : Curry<(...p: G) => R>;
78export {};