# 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); // => falseand(0, 'a'); // => 0and('', false); // => ''and([], 0); // => 0and(() => {}, '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') == []; // errorand(1, 'a') + '!' // no errorand(1)('a') + '!' // no errorany("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

## More from Jonathan Boiser

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

## I’ve Switched To-Do Apps… and It’s the Best Thing I’ve Done

Get the Medium app