Merge pull request #17097 from sbking/master

ramda: Allow currying of type guards
This commit is contained in:
Yui
2017-06-10 21:09:37 -07:00
committed by GitHub
2 changed files with 64 additions and 1 deletions

View File

@@ -1,6 +1,10 @@
// Type definitions for ramda
// Project: https://github.com/donnut/typescript-ramda
// Definitions by: Erwin Poeze <https://github.com/donnut>, Matt DeKrey <https://github.com/mdekrey>, Liam Goodacre <https://github.com/LiamGoodacre>, Matt Dziuban <https://github.com/mrdziuban>
// Definitions by: Erwin Poeze <https://github.com/donnut>
// Matt DeKrey <https://github.com/mdekrey>
// Liam Goodacre <https://github.com/LiamGoodacre>
// Matt Dziuban <https://github.com/mrdziuban>
// Stephen King <https://github.com/sbking>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
@@ -75,6 +79,41 @@ declare namespace R {
}
// @see https://gist.github.com/donnut/fd56232da58d25ceecf1, comment by @albrow
interface CurriedTypeGuard2<T1, T2, R extends T2> {
(t1: T1): (t2: T2) => t2 is R;
(t1: T1, t2: T2): t2 is R;
}
interface CurriedTypeGuard3<T1, T2, T3, R extends T3> {
(t1: T1): CurriedTypeGuard2<T2, T3, R>;
(t1: T1, t2: T2): (t3: T3) => t3 is R;
(t1: T1, t2: T2, t3: T3): t3 is R;
}
interface CurriedTypeGuard4<T1, T2, T3, T4, R extends T4> {
(t1: T1): CurriedTypeGuard3<T2, T3, T4, R>;
(t1: T1, t2: T2): CurriedTypeGuard2<T3, T4, R>;
(t1: T1, t2: T2, t3: T3): (t4: T4) => t4 is R;
(t1: T1, t2: T2, t3: T3, t4: T4): t4 is R;
}
interface CurriedTypeGuard5<T1, T2, T3, T4, T5, R extends T5> {
(t1: T1): CurriedTypeGuard4<T2, T3, T4, T5, R>;
(t1: T1, t2: T2): CurriedTypeGuard3<T3, T4, T5, R>;
(t1: T1, t2: T2, t3: T3): CurriedTypeGuard2<T4, T5, R>;
(t1: T1, t2: T2, t3: T3, t4: T4): (t5: T5) => t5 is R;
(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5): t5 is R;
}
interface CurriedTypeGuard6<T1, T2, T3, T4, T5, T6, R extends T6> {
(t1: T1): CurriedTypeGuard5<T2, T3, T4, T5, T6, R>;
(t1: T1, t2: T2): CurriedTypeGuard4<T3, T4, T5, T6, R>;
(t1: T1, t2: T2, t3: T3): CurriedTypeGuard3<T4, T5, T6, R>;
(t1: T1, t2: T2, t3: T3, t4: T4): CurriedTypeGuard2<T5, T6, R>;
(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5): (t6: T6) => t6 is R;
(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6): t6 is R;
}
interface CurriedFunction2<T1, T2, R> {
(t1: T1): (t2: T2) => R;
(t1: T1, t2: T2): R;
@@ -392,6 +431,11 @@ declare namespace R {
* Returns a curried equivalent of the provided function. The curried function has two unusual capabilities.
* First, its arguments needn't be provided one at a time.
*/
curry<T1, T2, TResult extends T2>(fn: (a: T1, b: T2) => b is TResult): CurriedTypeGuard2<T1,T2, TResult>
curry<T1, T2, T3, TResult extends T3>(fn: (a: T1, b: T2, c: T3) => c is TResult): CurriedTypeGuard3<T1,T2, T3, TResult>
curry<T1, T2, T3, T4, TResult extends T4>(fn: (a: T1, b: T2, c: T3, d: T4) => d is TResult): CurriedTypeGuard4<T1,T2, T3, T4, TResult>
curry<T1, T2, T3, T4, T5, TResult extends T5>(fn: (a: T1, b: T2, c: T3, d: T4, e: T5) => e is TResult): CurriedTypeGuard5<T1,T2, T3, T4, T5, TResult>
curry<T1, T2, T3, T4, T5, T6, TResult extends T6>(fn: (a: T1, b: T2, c: T3, d: T4, e: T5, f: T6) => f is TResult): CurriedTypeGuard6<T1,T2, T3, T4, T5, T6, TResult>
curry<T1, T2, TResult>(fn: (a: T1, b: T2) => TResult): CurriedFunction2<T1,T2, TResult>
curry<T1, T2, T3, TResult>(fn: (a: T1, b: T2, c: T3) => TResult): CurriedFunction3<T1,T2, T3, TResult>
curry<T1, T2, T3, T4, TResult>(fn: (a: T1, b: T2, c: T3, d: T4) => TResult): CurriedFunction4<T1,T2, T3, T4, TResult>

View File

@@ -86,6 +86,25 @@ class F2 {
var z2:number = addTwoNumbersCurried(2,3);
}
() => {
interface Car { speed?: number; }
interface FastCar { speed: number; }
const typeGuard = function(a: number, b: number, c: number, d: number, e: number, car: Car): car is FastCar {
return car.speed !== undefined;
}
const typeGuardCurried = R.curry(typeGuard);
const drive = function(fastCar: FastCar) {};
const cars: Car[] = [{ speed: 65 }, {}];
for (const car of cars) {
if (typeGuardCurried(1)(2)(3)(4)(5)(car)) {
drive(car);
}
}
}
() => {
const addFour = (a:number) => (b:number) => (c:number) => (d:number) => a + b + c + d;
const uncurriedAddFour = R.uncurryN<number>(4, addFour);