Skip to content

Commit

Permalink
fix(ramda): bump ts-toolbelt version, restore broken examples (Defini…
Browse files Browse the repository at this point in the history
…telyTyped#37849)

* revert broken curry tests, upgrade ts-toolbelt

* fix typo

* fix typo
  • Loading branch information
millsp authored and sheetalkamat committed Aug 23, 2019
1 parent c6faa19 commit ee45d6c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 120 deletions.
2 changes: 1 addition & 1 deletion types/ramda/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"dependencies": {
"ts-toolbelt": "^3.2.22"
"ts-toolbelt": "^3.8.4"
}
}
22 changes: 14 additions & 8 deletions types/ramda/ramda-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,20 @@ class F2 {

const x1: (a: number, b: number, c: number, d: number) => number = R.curry(addFourNumbers);
// because of the current way of currying, the following call results in a type error
// const x2: Function = R.curry(addFourNumbers)(1,2,4)
// const x3: (c: number, d: number) => number = R.curry(addFourNumbers)(1)(2);
// const x4: (d: number) => number = R.curry(addFourNumbers)(1)(2)(3);
const y1: number = R.curry(addFourNumbers)(1)(2)(3)(4);
const y2: number = R.curry(addFourNumbers)(1, 2)(3, 4);
const y3: number = R.curry(addFourNumbers)(1, 2, 3)(4);
const y4: number = R.curry(addTenFixedNumbers)(R.__, 1, 2)(0)(3)(R.__, R.__)(R.__, 5)(4)(6, 7)(R.__)(8, R.__, R.__)(9, 10);
const y5: number = R.curry(addTenFixedNumbers)(R.__, 1, R.__)(R.__, 2)(0, 3)(R.__, 5)(4, R.__)(R.__)(6, R.__, 8, 9, 10)(7);
const x2: (...args: any) => any = R.curry(addFourNumbers)(1, 2, 4);
const x3: (c: number, d: number) => number = R.curry(addFourNumbers)(1)(2);
const x4: (d: number) => number = R.curry(addFourNumbers)(1)(2)(3);
const y1: number = R.curry(addFourNumbers)(1)(2)(3)(4);
const y2: number = R.curry(addFourNumbers)(1, 2)(3, 4);
const y3: number = R.curry(addFourNumbers)(1, 2, 3)(4);
const y4: number = R.curry(addTenFixedNumbers)(R.__, 1, 2)(0)(3)(
R.__,
R.__,
)(R.__, 5)(4)(6, 7)(R.__)(8, R.__, R.__)(9, 10);
const y5: number = R.curry(addTenFixedNumbers)(R.__, 1, R.__)(R.__, 2)(
0,
3,
)(R.__, 5)(4, R.__)(R.__)(6, R.__, 8, 9, 10)(7);

R.nAry(0);
R.nAry(0, takesNoArg);
Expand Down
111 changes: 0 additions & 111 deletions types/ramda/tools.d.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,5 @@
/// <reference path="./index.d.ts" />

// All the following types are explained here:
// https://medium.freecodecamp.org/typescript-curry-ramda-types-f747e99744ab
// https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts
declare namespace Tools {
type Head<T extends any[]> =
T extends [any, ...any[]]
? T[0]
: never;

type Tail<T extends any[]> =
((...t: T) => any) extends ((_: any, ...tail: infer TT) => any)
? TT
: [];

type HasTail<T extends any[]> =
T extends ([] | [any])
? false
: true;

type Last<T extends any[]> = {
0: Last<Tail<T>>;
1: Head<T>;
}[
HasTail<T> extends true
? 0
: 1
];

type Length<T extends any[]> =
T['length'];

type Prepend<E, T extends any[]> =
((head: E, ...args: T) => any) extends ((...args: infer U) => any)
? U
: T;

type Drop<N extends number, T extends any[], I extends any[] = []> = {
0: Drop<N, Tail<T>, Prepend<any, I>>;
1: T;
}[
Length<I> extends N
? 1
: 0
];

type Cast<X, Y> = X extends Y ? X : Y;

type Pos<I extends any[]> =
Length<I>;

type Next<I extends any[]> =
Prepend<any, I>;

type Prev<I extends any[]> =
Tail<I>;

type Iterator<Index extends number = 0, From extends any[] = [], I extends any[] = []> = {
0: Iterator<Index, Next<From>, Next<I>>;
1: From;
}[
Pos<I> extends Index
? 1
: 0
];

type Reverse<T extends any[], R extends any[] = [], I extends any[] = []> = {
0: Reverse<T, Prepend<T[Pos<I>], R>, Next<I>>;
1: R;
}[
Pos<I> extends Length<T>
? 1
: 0
];

type Concat<T1 extends any[], T2 extends any[]> =
Reverse<Reverse<T1> extends infer R ? Cast<R, any[]> : never, T2>;

type Append<E, T extends any[]> =
Concat<T, [E]>;

type ValueOfRecord<R> = R extends Record<any, infer T> ? T : never;
}

declare namespace Curry {
type GapOf<T1 extends any[], T2 extends any[], TN extends any[], I extends any[]> =
T1[Tools.Pos<I>] extends R.Placeholder
? Tools.Append<T2[Tools.Pos<I>], TN>
: TN;

type GapsOf<T1 extends any[], T2 extends any[], TN extends any[] = [], I extends any[] = []> = {
0: GapsOf<T1, T2, GapOf<T1, T2, TN, I> extends infer G ? Tools.Cast<G, any[]> : never, Tools.Next<I>>;
1: Tools.Concat<TN, Tools.Drop<Tools.Pos<I>, T2> extends infer D ? Tools.Cast<D, any[]> : never>;
}[
Tools.Pos<I> extends Tools.Length<T1>
? 1
: 0
];

type PartialGaps<T extends any[]> = {
[K in keyof T]?: T[K] | R.Placeholder
};

type CleanedGaps<T extends any[]> = {
[K in keyof T]: NonNullable<T[K]>
};

type Gaps<T extends any[]> = CleanedGaps<PartialGaps<T>>;

type Curry<F extends ((...args: any) => any)> =
<T extends any[]>(...args: Tools.Cast<Tools.Cast<T, Gaps<Parameters<F>>>, any[]>) =>
GapsOf<T, Parameters<F>> extends [any, ...any[]]
? Curry<(...args: GapsOf<T, Parameters<F>> extends infer G ? Tools.Cast<G, any[]> : never) => ReturnType<F>>
: ReturnType<F>;
}

0 comments on commit ee45d6c

Please sign in to comment.