-
Notifications
You must be signed in to change notification settings - Fork 0
/
00017-hard-currying-1.ts
31 lines (25 loc) · 1004 Bytes
/
00017-hard-currying-1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
const curried1 = Currying((a: string, b: number, c: boolean) => true)
const curried2 = Currying((a: string, b: number, c: boolean, d: boolean, e: boolean, f: string, g: boolean) => true)
const curried3 = Currying(() => true)
type cases = [
Expect<Equal<
typeof curried1, (a: string) => (b: number) => (c: boolean) => true
>>,
Expect<Equal<
typeof curried2, (a: string) => (b: number) => (c: boolean) => (d: boolean) => (e: boolean) => (f: string) => (g: boolean) => true
>>,
Expect<Equal<typeof curried3, () => true>>,
]
// ============= Your Code Here =============
declare function Currying<F>(fn: F): F extends (...args: infer Args) => infer R
? CurringFunction<Args, R>
: never
type CurringFunction<P extends any[], R> = P extends []
? () => R
: P extends [infer F,...infer Rest]
? Rest['length'] extends 0
? (arg: F) => R
: (arg: F) => CurringFunction<Rest, R>
: never