Writing Flowtype annotations for Ramda: Polymorphism and R.any

Writing more generic types with polymorphism

We’re finally going to put add and subtract to rest, but transition into a discussion on polymorphism by defining NumericFn2 type in terms of the more general type CurriedFn2:

declare type CurriedFn2<T1, T2, R> =
& ((t1: T1, t2: T2) => R)
& ((t1: T1, ...rest: Array<void>) => (t2: T2) => R)

declare type NumericFn2 = CurriedFn2<number, number, number>
declare type CurriedFn2WithNumber<number, number, number> =
& ((t1: number, t2: number) => number)
& ((t1: number, ...rest: Array<void>) => (t2: number) => number)

Defining a type for R.any

The Ramda documentation for any describes it as a function that “Returns true if both arguments are true; false otherwise.” However, it gives the type signature as * → * → *, instead of the more restrictive boolean → boolean → boolean.

and(true, false); // => false
and(0, 'a'); // => 0
and('', false); // => ''
and([], 0); // => 0
and(() => {}, 'function'); // => 'function'
declare var and: CurriedFn2<any, any, any>
and(1, 'a') == [];
declare function and<T1, T2>(x: T1, y: T2, ...rest: Array<void>): T1 | T2;
and(1, 'a') == [];
// 12: a == [];
// ^ number. Cannot be compared to
// 12: a == [];
// ^ empty array literal
// 12: a == [];
// ^ string. Cannot be compared to
// 12: a == [];
// ^ empty array literal
declare function and<T1, T2>(x: T1, ...rest: Array<void>): (y: T2) => T1 | T2;
declare function and<T1, T2>(x: T1, y: T2, ...rest: Array<void>): T1 | T2;
and(1, 'a') == []; // error
and(1, 'a') + '!' // no error
and(1)('a') + '!' // no error
any("ada")([0,0]).concat("A") // no error
any(["a", "a"])([0,0]).concat("A")
// 19: any(["a", "a"])([0,0]).concat("A")
// ^ string. This type is incompatible with
// 19: any(["a", "a"])([0,0]).concat("A")
// ^ number
// 19: any(["a", "a"])([0,0]).concat("A")
// ^ number. This type is incompatible with
// 19: any(["a", "a"])([0,0]).concat("A")
// ^ string

What’s Next?

In the next post, we’ll start looking at functions for lists (e.g. map, filter) in Ramda.

Developer in San Diego, Ph.D. candidate in Bro Studies from Syracuse

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store