From 26d632b5f4433ec98028e2037950614be644d151 Mon Sep 17 00:00:00 2001 From: Stephen King Date: Fri, 9 Jun 2017 15:44:58 -0600 Subject: [PATCH] ramda: Allow currying of type guards --- types/ramda/index.d.ts | 46 +++++++++++++++++++++++++++++++++++++- types/ramda/ramda-tests.ts | 19 ++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/types/ramda/index.d.ts b/types/ramda/index.d.ts index 9666c1b78f..79d1b1f6cf 100644 --- a/types/ramda/index.d.ts +++ b/types/ramda/index.d.ts @@ -1,6 +1,10 @@ // Type definitions for ramda // Project: https://github.com/donnut/typescript-ramda -// Definitions by: Erwin Poeze , Matt DeKrey , Liam Goodacre , Matt Dziuban +// Definitions by: Erwin Poeze +// Matt DeKrey +// Liam Goodacre +// Matt Dziuban +// Stephen King // 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: T1): (t2: T2) => t2 is R; + (t1: T1, t2: T2): t2 is R; + } + + interface CurriedTypeGuard3 { + (t1: T1): CurriedTypeGuard2; + (t1: T1, t2: T2): (t3: T3) => t3 is R; + (t1: T1, t2: T2, t3: T3): t3 is R; + } + + interface CurriedTypeGuard4 { + (t1: T1): CurriedTypeGuard3; + (t1: T1, t2: T2): CurriedTypeGuard2; + (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: T1): CurriedTypeGuard4; + (t1: T1, t2: T2): CurriedTypeGuard3; + (t1: T1, t2: T2, t3: T3): CurriedTypeGuard2; + (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: T1): CurriedTypeGuard5; + (t1: T1, t2: T2): CurriedTypeGuard4; + (t1: T1, t2: T2, t3: T3): CurriedTypeGuard3; + (t1: T1, t2: T2, t3: T3, t4: T4): CurriedTypeGuard2; + (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: 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(fn: (a: T1, b: T2) => b is TResult): CurriedTypeGuard2 + curry(fn: (a: T1, b: T2, c: T3) => c is TResult): CurriedTypeGuard3 + curry(fn: (a: T1, b: T2, c: T3, d: T4) => d is TResult): CurriedTypeGuard4 + curry(fn: (a: T1, b: T2, c: T3, d: T4, e: T5) => e is TResult): CurriedTypeGuard5 + curry(fn: (a: T1, b: T2, c: T3, d: T4, e: T5, f: T6) => f is TResult): CurriedTypeGuard6 curry(fn: (a: T1, b: T2) => TResult): CurriedFunction2 curry(fn: (a: T1, b: T2, c: T3) => TResult): CurriedFunction3 curry(fn: (a: T1, b: T2, c: T3, d: T4) => TResult): CurriedFunction4 diff --git a/types/ramda/ramda-tests.ts b/types/ramda/ramda-tests.ts index f9b272f8ca..8fbbb5c7ba 100644 --- a/types/ramda/ramda-tests.ts +++ b/types/ramda/ramda-tests.ts @@ -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(4, addFour);