Reputation: 682
I encountered a js lib which provided the external ts types. Assuming it's
interface ComplicatedFunction {
(n: number, p: any): string,
(s: string, p: any): number,
(b: boolean, p: any): number,
}
declare const fn: ComplicatedFunction
I want to curry it:
import fn from 'fn'
const curriedFn = (p1: ?) =>
(p2: any) =>
fn(p1, p2)
The question is what is how to fill the p1's type? (all strict checks on)
I tried using generic to fix the types:
const curry2 = <P1, P2, R>(fn: (n: P1, m: P2) => R) =>
(n: P1) => (m: P2) => fn(n, m)
const curriedFn = curry2(fn)
unfortunately, the curriedFn
falls into signature (b: boolean, p: any): number
. The other two function signatures are omitted by this way.
Upvotes: 3
Views: 1217
Reputation: 249556
Since the function has some parameters that decide the result type, you will need to use a similar type for the curried function. Unfortunately, type inference will not help you here, you will need to specify the type manually, and use some extra type assertions when calling the original function:
const curriedFn : {
(n: number): (p: any) => string,
(s: string): (p: any) => number,
(b: boolean): (p: any) => number,
} = (p1: number | string | boolean) => (p2: any) => <any>fn(<any>p1, p2)
Upvotes: 1